WPF互斥单一应用程序实例不工作 [英] WPF mutex for single app instance not working
问题描述
我试图用互斥方法只允许我的应用程序运行的一个实例。那就是 - 我只想要一个实例的计算机上的所有用户最大。我已经通过各种其他线程在这个问题上阅读和解决方案似乎很简单,但在测试中,我不能让我的第二个实例不运行。这里是我的代码...
公共部分类应用:应用
{
保护覆盖无效OnStartup (StartupEventArgs E)
{
//检查是否有运行控制面板只有一个实例...
布尔createdNew = TRUE;
使用(互斥instanceMutex =新的Mutex(真的,@Global\ControlPanel,出createdNew))
{
如果(!createdNew)
{
应用.Current.Shutdown();
的回报;
}
}
base.OnStartup(E);
}
}
您'重新也设置在相同的方法互斥,所以互斥只住该方法的持续时间。存储互斥的静态字段,并保持它活着你的应用程序的时间。
I'm trying to use the mutex method for only allowing one instance of my app to run. That is - I only want a max of one instance for all users on a machine. I've read through the various other threads on this issue and the solution seems straightforward enough but in testing I can't get my second instance to not run. Here is my code...
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
// check that there is only one instance of the control panel running...
bool createdNew = true;
using (Mutex instanceMutex = new Mutex(true, @"Global\ControlPanel", out createdNew))
{
if (!createdNew)
{
Application.Current.Shutdown();
return;
}
}
base.OnStartup(e);
}
}
You're also disposing the mutex in the same method, so the mutex only lives for the duration of the method. Store the mutex in a static field, and keep it alive for the duration of your application.
这篇关于WPF互斥单一应用程序实例不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!