using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Threading; namespace WindowsFormsApplication1 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); bool ret; Mutex mutex = new Mutex(true, Application.ProductName, out ret); if (ret) { Application.Run(new Form1()); } else { MessageBox.Show("該程序已經啟動", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }
應用程序多次啟動會因為資源占用等問題對程序的正常運行產生影響,在某些情況下需要對程序的啟動次數進行限制。紅色部分代碼的作用是避免程序重復啟動。
