diff --git a/RecycleBin/App.xaml.cs b/RecycleBin/App.xaml.cs index d374a5f..ae3c3aa 100644 --- a/RecycleBin/App.xaml.cs +++ b/RecycleBin/App.xaml.cs @@ -1,4 +1,6 @@ -using System.Windows; +using System.Diagnostics; +using System.Threading; +using System.Windows; namespace RecycleBin { @@ -7,13 +9,28 @@ namespace RecycleBin /// public partial class App { + private static Mutex _mutex; private readonly Tray _tray = new Tray(); private void ApplicationStartup(object sender, StartupEventArgs e) { + CheckRunning(); + _tray.CreateTrayIcon(); } + /// + /// 检测是否已经有相同程序在运行。如果存在,则退出当前程序 + /// + private static void CheckRunning() + { + bool noRun; + _mutex = new Mutex(true, "RecycleBin", out noRun); + + if (noRun) _mutex.ReleaseMutex(); + else Process.GetCurrentProcess().Kill(); + } + // 程序退出 private void ApplicationExit(object sender, ExitEventArgs e) {