Mutex Check

master
Tongg 2 weeks ago
parent a649ac4204
commit a347bfad31
  1. 24
      App.xaml.cs
  2. 8
      MainWindow.xaml.cs
  3. 9
      Model/Exceptions/ActiveCloseException.cs
  4. 44
      Model/Helper/ProcessHelper.cs

@ -4,6 +4,7 @@ using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using BrilliantSightClient.Model.Exceptions;
using log4net;
using log4net.Config;
using BrilliantSightClient.Model.Helper;
@ -17,13 +18,29 @@ namespace BrilliantSightClient;
public partial class App : Application
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(App));
private static Mutex? _mutex = null;
protected override void OnStartup(StartupEventArgs e)
{
const string appName = "BrilliantSight";
bool createdNew;
_mutex = new Mutex(true, $"Global\\{{{appName}}}", out createdNew);
if (!createdNew)
{
MessageBox.Show("An instance of this application is already running.",
"BrilliantSight Is Running",
MessageBoxButton.OK,
MessageBoxImage.Warning);
Shutdown();
return;
}
base.OnStartup(e);
//加载日志配置文件
XmlConfigurator.Configure(new FileInfo("log4net.config"));
Logger.Info("==== ==== ==== ==== ==== ==== ==== ====");
Logger.Info("App Client Start: App Initialize");
Logger.Info("Loaded Logs Config: log4net.config");
//运行是否需要归档
Log4NetHelper.ArchiveOldLogs();
@ -56,10 +73,17 @@ public partial class App : Application
Logger.Info("App Client Start: App Initialize Succeed !!!");
DispatcherUnhandledException += (s, ex) =>
{
MessageBox.Show($"致命错误: {ex.Exception.Message}");
Logger.Error($"发生致命错误!!!{ex.Exception.Message}{ex.Exception.StackTrace}");
ex.Handled = true;
};
Logger.Info("==== ==== ==== ==== ==== ==== ==== ====");
}
protected override void OnExit(ExitEventArgs e)
{
_mutex?.ReleaseMutex();
base.OnExit(e);
}
}

@ -1,5 +1,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
@ -11,6 +13,7 @@ using HandyControl.Tools;
using log4net;
using log4net.Config;
using BrilliantSightClient.Model.Attributes;
using BrilliantSightClient.Model.Exceptions;
using BrilliantSightClient.Model.Helper;
using BrilliantSightClient.Model.Services;
using BrilliantSightClient.ViewModel;
@ -158,9 +161,8 @@ public partial class MainWindow
{
SOCClientService.Service.OpenPump(false);
DataBaseHelper.CloseConnection();
// this.Close();
//Environment.Exit(0);
Console.WriteLine("Shutdown");
Logger.Info("Shutdown");
ProcessHelper.TerminateProcessByName(Process.GetCurrentProcess().ProcessName);
Application.Current.Shutdown();
}
e.Cancel = true;

@ -0,0 +1,9 @@
namespace BrilliantSightClient.Model.Exceptions;
public class ActiveCloseException : Exception
{
public ActiveCloseException()
{
}
}

@ -0,0 +1,44 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace BrilliantSightClient.Model.Helper;
public class ProcessHelper
{
private static extern bool CloseHandle(IntPtr hObject);
// 进程访问权限
private const int PROCESS_TERMINATE = 0x0001;
// 根据进程名查找并终止进程
public static void TerminateProcessByName(string processName)
{
try
{
Process[] processes = Process.GetProcessesByName(processName);
foreach (Process process in processes)
{
try
{
process.Kill();
process.WaitForExit();
}
catch (Exception ex)
{
// 可以选择记录错误但不显示给用户
Debug.WriteLine($"结束进程出错: {ex.Message}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"查找进程时出错: {ex.Message}");
}
}
}
Loading…
Cancel
Save