AHAQ_QGNH/AnQing/App.xaml.cs
2025-05-19 09:31:47 +08:00

62 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace AnQing
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
private static Mutex _mutex;
private const string AppName = "AnQing";
public static bool IsDuplicateInstance { get; private set; } = false; // 标记重复运行
protected override void OnStartup(StartupEventArgs e)
{
bool createdNew;
_mutex = new Mutex(true, "Global\\" + AppName, out createdNew);
if (!createdNew)
{
// 标记为重复运行
IsDuplicateInstance = true;
// 显示提示
MessageBox.Show("程序已在运行中!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
// 直接关闭应用
Application.Current.Shutdown();
return;
}
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
_mutex?.ReleaseMutex();
_mutex?.Dispose();
base.OnExit(e);
}
public static void Restart()
{
// 释放互斥锁
_mutex.ReleaseMutex();
string executablePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AnQing.exe");
System.Diagnostics.Process.Start(executablePath);
//System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
Application.Current.Shutdown();
}
}
}