WayneShao 的博客

记录精彩的程序人生

【单例】使用同步基元变量来检测程序是否已运行

static class Program
{
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
        var instance = new Mutex(true, "SingleStart", out bool createdNew); //同步基元变量   
        if (createdNew)
        {
            Application.Run(new Form());
            instance.ReleaseMutex();
        }
        else
        {
            Application.Exit();
        }
     }
}
留下你的脚步