用互斥量Mutex实现
如果已经存在Mutex,则会创建失败。
注意:Mutex要声明成全局的,不能是局部变量,否则会判断失败。
重写Startup函数,加上单例判断。
参考下面代码:
1 public partial class App : Application 2 { 3 System.Threading.Mutex mutex; 4 5 protected override void OnStartup(StartupEventArgs e) 6 { 7 if (CheckSingleInstance() == false) 8 Application.Current.Shutdown(0); 9 10 base.OnStartup(e); 11 } 12 13 private bool CheckSingleInstance() 14 { 15 bool isSingleInstance; 16 mutex = new System.Threading.Mutex(true, "MutexName", out isSingleInstance); 17 return isSingleInstance; 18 } 19 }
标签:OnStartup,System,实例,Mutex,isSingleInstance,进程,mutex,WPF,bool From: https://www.cnblogs.com/zhaotianff/p/17411156.html