From 5bb90828f85591fe07f931c05ad76f56c615f256 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Sun, 29 Nov 2015 03:28:47 +0000 Subject: [PATCH] Fix Wox restart by wait Mutex to be released Fix bug in 24866ff032829e9bd34704d3d5970d227a4c8db3. Wait existing Mutex to release itself instead of create a new one. Relate issue: #322 --- Wox/App.xaml.cs | 8 +++++--- Wox/Helper/SingleInstance.cs | 22 +++++++--------------- Wox/MainWindow.xaml.cs | 8 ++------ Wox/Wox.csproj | 2 +- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index de80657c37..2d950b6892 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -50,14 +50,16 @@ namespace Wox AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle; } - public bool OnActivate(IList args) + public void OnActivate(IList args) { if (args.Count > 0 && args[0] == SingleInstance.Restart) { Window.CloseApp(); } - CommandArgsFactory.Execute(args); - return true; + else + { + CommandArgsFactory.Execute(args); + } } } } diff --git a/Wox/Helper/SingleInstance.cs b/Wox/Helper/SingleInstance.cs index bd68270ffa..20a51fefa5 100644 --- a/Wox/Helper/SingleInstance.cs +++ b/Wox/Helper/SingleInstance.cs @@ -1,14 +1,4 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -// -// This class checks to make sure that only one instance of -// this application is running at a time. -// -//----------------------------------------------------------------------- - -using System; +using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; @@ -23,7 +13,8 @@ using System.Threading; using System.Windows; using System.Windows.Threading; -//http://blogs.microsoft.co.il/arik/2010/05/28/wpf-single-instance-application/ +// http://blogs.microsoft.co.il/arik/2010/05/28/wpf-single-instance-application/ +// modified to allow single instace restart namespace Wox.Helper { internal enum WM @@ -194,7 +185,7 @@ namespace Wox.Helper public interface ISingleInstanceApp { - bool OnActivate(IList args); + void OnActivate(IList args); } /// @@ -239,7 +230,7 @@ namespace Wox.Helper /// /// Application mutex. /// - private static Mutex singleInstanceMutex; + internal static Mutex singleInstanceMutex; /// /// IPC channel for communications. @@ -291,10 +282,11 @@ namespace Wox.Helper CreateRemoteService(channelName); return true; } + // Restart else if (commandLineArgs.Count > 0 && commandLineArgs[0] == Restart) { SignalFirstInstance(channelName, commandLineArgs); - singleInstanceMutex = new Mutex(true, applicationIdentifier); + singleInstanceMutex.WaitOne(TimeSpan.FromSeconds(10)); CreateRemoteService(channelName); return true; } diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index f25e51e822..7f2aaf60a4 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -80,12 +80,8 @@ namespace Wox public void CloseApp() { - Dispatcher.Invoke(new Action(() => - { - notifyIcon.Visible = false; - Close(); - Environment.Exit(0); - })); + SingleInstance.singleInstanceMutex.ReleaseMutex(); + Application.Current.Shutdown(); } public void RestarApp() diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index c906ccb385..ecd36eeda0 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -1,4 +1,4 @@ - +