WPF程序重新启动,如果有客户端进程存在,则关闭较早的进程
1 private static System.Threading.Mutex mutex; 2 public App() 3 { 4 this.Startup += new StartupEventHandler(App_Startup); 5 } 6 /// <summary> 7 /// 登陆前判断是否存在客户端进程 8 /// </summary> 9 void App_Startup(object sender, StartupEventArgs e) 10 { 11 bool ret; 12 mutex = new System.Threading.Mutex(true, "TCClient", out ret); 13 Logger.D("打印有没有重复进程:",(!ret).ToString()); 14 if (!ret) 15 { 16 Process process = Process.GetCurrentProcess();//后运行的程序 17 foreach (Process p in Process.GetProcessesByName("TCClient")) 18 { 19 //不是同一进程并且本进程启动时间最晚,则关闭较早进程 20 if (p.Id != process.Id && (p.StartTime - process.StartTime).TotalMilliseconds <= 0) 21 { 22 p.Kill(); 23 //Application.Current.Shutdown(-1); 24 Thread.Sleep(500); 25 return; 26 } 27 } 28 29 //MessageBox.Show("已存在一个本客户端进程在运行,请手动关闭后再重新启动", "系统提示", MessageBoxButton.OK); 30 //Environment.Exit(0); 31 } 32 33 }