互斥锁终止应用程序
本文关键字:应用程序 终止 | 更新日期: 2023-09-27 18:05:46
这是我的代码:
protected override async void OnStartup(StartupEventArgs e)
{
// Used to check if we can create a new mutex
bool newMutexCreated = false;
try
{
// Create a new mutex object with a unique name
mutex = new Mutex(false, MutexName, out newMutexCreated);
}
catch (Exception ex)
when (ex is UnauthorizedAccessException ||
ex is IOException ||
ex is WaitHandleCannotBeOpenedException ||
ex is ArgumentException)
{
Logger.Error("Error while launching application. Failed to check for other instances.", ex);
Shutdown((int)ExitCode.ApplicationAlreadyRunning);
}
// When the mutex is created for the first time
// we run the program since it is the first instance.
if (newMutexCreated)
{
await ContinueStartup(e);
return;
}
else
{
// Otherwise we get the first instance with that process name,
Process[] currentProcesses = Process.GetProcessesByName(AssemblyName);
IntPtr mainWindowHandle = currentProcesses[0].MainWindowHandle;
if (mainWindowHandle != IntPtr.Zero)
{
// maximize it, if it was minimized, and set it to foreground.
Logger.Info("Another instance of the application is already running.");
ShowWindow(mainWindowHandle, WindowShowNormal);
SetForegroundWindow(mainWindowHandle);
}
// Then shutdown this instance.
Logger.Info("Shutting down.");
Shutdown((int)ConsoleModeExitCode.ApplicationAlreadyRunning);
}
}
protected override void OnExit(ExitEventArgs e)
{
Logger.Info("Exiting application.");
// Close mutex.
mutex.Dispose();
base.OnExit(e);
}
这里发生的是我的应用程序应该启动一次。当它运行时,每次尝试启动一个新实例都应该将第一个实例带到前面。
但实际发生的是:在2-10次启动尝试后,第一个实例的GUI被杀死,进程仍在运行并阻塞互斥锁,只能在TaskManager中被杀死。如果我尝试调试这种行为并在VisualStudio中运行应用程序,它就永远不会发生。尝试打开应用程序50次不会杀死它,所以我无法跟踪似乎发生的事件。
这是垃圾收集器的正常行为吗?它杀死了第一个实例,以防它被绞死?还是我错过了什么?
好吧,正如@Luaan提到的,问题不在于互斥锁。我用这个解决方案修复了我的代码:
https://stackoverflow.com/a/9059657/3319147 ShowWindowAsync
和对句柄的intptr值的稍微不同的处理似乎使这种方式更稳定。从那以后就再也不能崩溃了。对我来说,这是足够的稳定性:)