防止一個exe被打開屢次

mutex有一個名字,若是這個exe已經打開了,createNew返回的就是false,程序就退出了。app

這是個wpf application的例子      ide

protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            bool createdNew;
            Mutex mutex = new Mutex(true, "wpfapp28", out createdNew);
            if (createdNew)
            {

            }
            else
            {
                MessageBox.Show("already open");
                App.Current.Shutdown();
            }
        }io

相關文章
相關標籤/搜索