有的WPF开发的程序要限制只能打开一个实例。打开App.xaml.cs,加入如下代码:
public partial class App : Application
{
System.Threading.Mutex mutex;
public App()
{
this.Startup += new StartupEventHandler(App_Startup);
}
void App_Startup(object sender, StartupEventArgs e)
{
bool ret;
mutex = new System.Threading.Mutex(true, "ElectronicNeedleTherapySystem", out ret);
if (!ret)
{
MessageBox.Show("已经启动请先关闭之后再打开!");
Environment.Exit(0);
}
}
}


在WPF应用程序中,通过在App.xaml.cs中添加代码,使用Mutex对象可以实现只允许一个实例运行的功能。当尝试打开第二个实例时,系统会检测到Mutex的存在并提示用户已有实例正在运行,从而防止多个实例同时打开。

1804

被折叠的 条评论
为什么被折叠?



