设置只能开启一个程序实例

在“Program.cs”中添加以下代码:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    bool blnIsRunning;
    Mutex mutexApp = new Mutex(false, Assembly.GetExecutingAssembly().FullName, out blnIsRunning);
    if (!blnIsRunning)
    {
        MessageBox.Show("Is Running!", "Warning",
        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return;
    }
    Application.Run(new Form1());
}

测试结果:

posted @ 2019-04-25 14:03  Kyle0418  阅读(181)  评论(0编辑  收藏  举报