Mutex杀死了应用程序

时间:2016-09-05 12:17:17

标签: c# mutex

这是我的代码:

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被终止,进程仍然在运行并阻塞Mutex,只能在TaskManager中被杀死。如果我尝试调试此行为并在VisualStudio中运行该应用程序,它就永远不会发生。试图打开应用程序50次永远不会杀死它,所以我无法跟踪似乎发生的事件。

GarbageCollector的正常行为是什么?如果挂起它会杀死第一个实例? 或者我错过了什么?

1 个答案:

答案 0 :(得分:0)

好的,正如@Luaan所说,问题不在于Mutex。 我用这个解决方案修复了我的代码:

https://stackoverflow.com/a/9059657/3319147

ShowWindowAsync并且对句柄的IntPtr值稍有不同的处理似乎使方式更加稳定。从那以后无法崩溃。对我来说这是足够的稳定性:))