(單例)使用同步基元變量來檢測程序是否已運行

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();
        }
     }
}
相關文章
相關標籤/搜索