fix SingleInstance

This commit is contained in:
bao-qian
2016-05-09 22:45:20 +01:00
parent 21285f02b8
commit fb3ba16a92
12 changed files with 56 additions and 192 deletions

View File

@@ -10,11 +10,10 @@ using Stopwatch = Wox.Infrastructure.Stopwatch;
namespace Wox
{
public partial class App : ISingleInstanceApp, IDisposable
public partial class App : IDisposable, ISingleInstanceApp
{
private const string Unique = "Wox_Unique_Application_Mutex";
public static MainWindow Window { get; private set; }
public static PublicAPIInstance API { get; private set; }
private const string Unique = "Wox_Unique_Application_Mutex";
private static bool _disposed;
[STAThread]
@@ -44,10 +43,14 @@ namespace Wox
API = new PublicAPIInstance(mainVM, mainVM._settings);
PluginManager.InitializePlugins(API, pluginsSettings);
Window = new MainWindow(mainVM._settings, mainVM);
var _notifyIconManager = new NotifyIconManager(API);
var window = new MainWindow(mainVM._settings, mainVM);
RegisterExitEvents();
Current.MainWindow = window;
Current.MainWindow.Title = Infrastructure.Wox.Name;
window.Show();
});
}
@@ -76,30 +79,21 @@ namespace Wox
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
}
public void OnActivate()
{
API.ShowApp();
}
private static void Save()
{
var vm = (MainViewModel)Window.DataContext;
vm.Save();
PluginManager.Save();
ImageLoader.Save();
_disposed = true;
}
public void Dispose()
{
// if sessionending is called, exit proverbially be called when log off / shutdown
// but if sessionending is not called, exit won't be called when log off / shutdown
if (!_disposed)
{
Save();
SingleInstance<App>.Cleanup();
var vm = (MainViewModel)Current.MainWindow.DataContext;
vm.Save();
_disposed = true;
}
}
public void OnSecondAppStarted()
{
API.ShowApp();
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Windows;
using System.Windows.Threading;
using Wox.Infrastructure.Exception;
using Wox.Infrastructure.Logger;
@@ -16,7 +17,7 @@ namespace Wox.Helper
public static void UnhandledExceptionHandle(object sender, UnhandledExceptionEventArgs e)
{
//handle non-ui thread exceptions
App.Window.Dispatcher.Invoke(() =>
Application.Current.MainWindow.Dispatcher.Invoke(() =>
{
Report((Exception)e.ExceptionObject);
if (!(e.ExceptionObject is WoxException))

View File

@@ -186,7 +186,7 @@ namespace Wox.Helper
public interface ISingleInstanceApp
{
void OnActivate();
void OnSecondAppStarted();
}
/// <summary>
@@ -416,7 +416,7 @@ namespace Wox.Helper
return;
}
((TApplication)Application.Current).OnActivate();
((TApplication)Application.Current).OnSecondAppStarted();
}
#endregion
@@ -437,8 +437,7 @@ namespace Wox.Helper
if (Application.Current != null)
{
// Do an asynchronous call to ActivateFirstInstance function
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Normal, new DispatcherOperationCallback(ActivateFirstInstanceCallback));
Application.Current.Dispatcher.Invoke(ActivateFirstInstance);
}
}

View File

@@ -10,6 +10,7 @@ using Wox.Core.Resource;
using Wox.Core.UserSettings;
using Wox.Helper;
using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Logger;
using Wox.ViewModel;
using DataFormats = System.Windows.DataFormats;
using DragEventArgs = System.Windows.DragEventArgs;
@@ -42,8 +43,12 @@ namespace Wox
{
_settings.WindowLeft = Left;
_settings.WindowTop = Top;
var vm = (MainViewModel) DataContext;
vm.Save();
}
private void OnLoaded(object sender, RoutedEventArgs _)
{
InitProgressbarAnimation();

View File

@@ -63,16 +63,17 @@ namespace Wox
public void CloseApp()
{
//notifyIcon.Visible = false;
Application.Current.Shutdown();
Application.Current.MainWindow.Close();
}
public void RestarApp()
{
HideWox();
// we must force dispose application
// we must manually save
// UpdateManager.RestartApp() will call Environment.Exit(0)
// which will cause ungraceful exit
((IDisposable) Application.Current).Dispose();
var vm = (MainViewModel) Application.Current.MainWindow.DataContext;
vm.Save();
UpdateManager.RestartApp();
}

View File

@@ -7,7 +7,6 @@ using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
@@ -24,18 +23,11 @@ using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Image;
using Wox.Plugin;
using Wox.ViewModel;
using Application = System.Windows.Forms.Application;
using CheckBox = System.Windows.Controls.CheckBox;
using Control = System.Windows.Controls.Control;
using Cursors = System.Windows.Input.Cursors;
using HorizontalAlignment = System.Windows.HorizontalAlignment;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MessageBox = System.Windows.MessageBox;
using Stopwatch = Wox.Infrastructure.Stopwatch;
namespace Wox
{
public partial class SettingWindow : Window
public partial class SettingWindow
{
public readonly IPublicAPI _api;
bool settingsLoaded;
@@ -227,7 +219,7 @@ namespace Wox
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
true))
{
key.SetValue("Wox", "\"" + Application.ExecutablePath + "\" --hidestart");
key.SetValue("Wox", "\"" + Infrastructure.Wox.ProgramPath + "\" --hidestart");
}
}
@@ -253,7 +245,7 @@ namespace Wox
private void SelectPythonDirectoryOnClick(object sender, RoutedEventArgs e)
{
var dlg = new FolderBrowserDialog
var dlg = new System.Windows.Forms.FolderBrowserDialog
{
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
};
@@ -289,7 +281,7 @@ namespace Wox
{
SetHotkey(ctlHotkey.CurrentHotkey, delegate
{
if (!App.Window.IsVisible)
if (!System.Windows.Application.Current.MainWindow.IsVisible)
{
_api.ShowApp();
}
@@ -433,48 +425,48 @@ namespace Wox
Title = "Wox is an effective launcher for windows",
SubTitle = "Wox provide bundles of features let you access infomations quickly.",
IcoPath = "Images/app.png",
PluginDirectory = Path.GetDirectoryName(Application.ExecutablePath)
PluginDirectory = Path.GetDirectoryName(Infrastructure.Wox.ProgramPath)
},
new Result
{
Title = "Search applications",
SubTitle = "Search applications, files (via everything plugin) and browser bookmarks",
IcoPath = "Images/app.png",
PluginDirectory = Path.GetDirectoryName(Application.ExecutablePath)
PluginDirectory = Path.GetDirectoryName(Infrastructure.Wox.ProgramPath)
},
new Result
{
Title = "Search web contents with shortcuts",
SubTitle = "e.g. search google with g keyword or youtube keyword)",
IcoPath = "Images/app.png",
PluginDirectory = Path.GetDirectoryName(Application.ExecutablePath)
PluginDirectory = Path.GetDirectoryName(Infrastructure.Wox.ProgramPath)
},
new Result
{
Title = "clipboard history ",
IcoPath = "Images/app.png",
PluginDirectory = Path.GetDirectoryName(Application.ExecutablePath)
PluginDirectory = Path.GetDirectoryName(Infrastructure.Wox.ProgramPath)
},
new Result
{
Title = "Themes support",
SubTitle = "get more themes from http://www.getwox.com/theme",
IcoPath = "Images/app.png",
PluginDirectory = Path.GetDirectoryName(Application.ExecutablePath)
PluginDirectory = Path.GetDirectoryName(Infrastructure.Wox.ProgramPath)
},
new Result
{
Title = "Plugins support",
SubTitle = "get more plugins from http://www.getwox.com/plugin",
IcoPath = "Images/app.png",
PluginDirectory = Path.GetDirectoryName(Application.ExecutablePath)
PluginDirectory = Path.GetDirectoryName(Infrastructure.Wox.ProgramPath)
},
new Result
{
Title = "Wox is an open-source software",
SubTitle = "Wox benefits from the open-source community a lot",
IcoPath = "Images/app.png",
PluginDirectory = Path.GetDirectoryName(Application.ExecutablePath)
PluginDirectory = Path.GetDirectoryName(Infrastructure.Wox.ProgramPath)
}
});

View File

@@ -11,6 +11,7 @@ using Wox.Core.UserSettings;
using Wox.Helper;
using Wox.Infrastructure;
using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Image;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using Wox.Storage;
@@ -47,6 +48,7 @@ namespace Wox.ViewModel
private CancellationTokenSource _updateSource;
private CancellationToken _updateToken;
private bool _saved;
#endregion
@@ -54,6 +56,7 @@ namespace Wox.ViewModel
public MainViewModel()
{
_saved = false;
_queryTextBeforeLoadContextMenu = "";
_queryText = "";
_lastQuery = new Query();
@@ -592,10 +595,18 @@ namespace Wox.ViewModel
public void Save()
{
_settingsStorage.Save();
_queryHistoryStorage.Save();
_userSelectedRecordStorage.Save();
_topMostRecordStorage.Save();
if (!_saved)
{
_settingsStorage.Save();
_queryHistoryStorage.Save();
_userSelectedRecordStorage.Save();
_topMostRecordStorage.Save();
PluginManager.Save();
ImageLoader.Save();
_saved = true;
}
}
/// <summary>

View File

@@ -432,8 +432,7 @@
</PostBuildEvent>
</PropertyGroup>
<PropertyGroup>
<PreBuildEvent>
</PreBuildEvent>
<PreBuildEvent>taskkill /f /fi "IMAGENAME eq Wox.exe</PreBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.