Skip to content

Commit

Permalink
启动时新增程序唯一性检测
Browse files Browse the repository at this point in the history
  • Loading branch information
luxin committed Jun 28, 2020
1 parent 60c7edd commit bc1c111
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion RecycleBin/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Windows;
using System.Diagnostics;
using System.Threading;
using System.Windows;

namespace RecycleBin
{
Expand All @@ -7,13 +9,28 @@ namespace RecycleBin
/// </summary>
public partial class App
{
private static Mutex _mutex;
private readonly Tray _tray = new Tray();

private void ApplicationStartup(object sender, StartupEventArgs e)
{
CheckRunning();

_tray.CreateTrayIcon();
}

/// <summary>
/// 检测是否已经有相同程序在运行。如果存在,则退出当前程序
/// </summary>
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)
{
Expand Down

0 comments on commit bc1c111

Please sign in to comment.