using System.Threading; using System.Windows; namespace BmsMonitor.App; public partial class App : Application { private Mutex? _singleInstanceMutex; private bool _ownsMutex; protected override void OnStartup(StartupEventArgs e) { _singleInstanceMutex = new Mutex(true, "Local\\BmsMonitor.ReadOnly.SingleInstance", out _ownsMutex); if (!_ownsMutex) { MessageBox.Show("BMS Monitor 已经在运行。请关闭已有窗口后再启动。", "程序已运行", MessageBoxButton.OK, MessageBoxImage.Information); Shutdown(); return; } base.OnStartup(e); } protected override void OnExit(ExitEventArgs e) { if (_ownsMutex) _singleInstanceMutex?.ReleaseMutex(); _singleInstanceMutex?.Dispose(); base.OnExit(e); } }