From 5d5ee40cd6574b8574378387e23340b119853c82 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Wed, 24 Mar 2021 11:31:25 +0800 Subject: [PATCH] Migrate to HMBSbige.SingleInstance --- Netch/Forms/MainForm.cs | 8 +++++- Netch/Global.cs | 6 ++--- Netch/Netch.cs | 53 +++++++++------------------------------- Netch/Netch.csproj | 1 + Netch/Updater/Updater.cs | 2 +- 5 files changed, 24 insertions(+), 46 deletions(-) diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 09529602..13b7caa2 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -1462,8 +1462,14 @@ namespace Netch.Forms #region NotifyIcon - private void ShowMainFormToolStripButton_Click(object sender, EventArgs e) + public void ShowMainFormToolStripButton_Click(object sender, EventArgs e) { + if (InvokeRequired) + { + Invoke(new Action(() => ShowMainFormToolStripButton_Click(sender, e))); + return; + } + if (WindowState == FormWindowState.Minimized) { Visible = true; diff --git a/Netch/Global.cs b/Netch/Global.cs index a769f0e9..5959f2eb 100644 --- a/Netch/Global.cs +++ b/Netch/Global.cs @@ -29,9 +29,7 @@ namespace Netch /// private static readonly Lazy LazyMainForm = new(() => new MainForm()); - private static readonly Lazy LazyMutex = new(() => new Mutex(false, "Global\\Netch")); - - public static Mutex Mutex => LazyMutex.Value; + public static SingleInstance.SingleInstance SingleInstance = new($"Global\\{nameof(Netch)}"); public static LogStopwatch LogStopwatch = null!; @@ -65,5 +63,7 @@ namespace Netch IgnoreNullValues = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; + + public const string ParameterShow = "-show"; } } \ No newline at end of file diff --git a/Netch/Netch.cs b/Netch/Netch.cs index b09db8e2..ff97f7d3 100644 --- a/Netch/Netch.cs +++ b/Netch/Netch.cs @@ -1,8 +1,7 @@ using System; -using System.Diagnostics; +using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -10,8 +9,6 @@ using Netch.Controllers; using Netch.Forms; using Netch.Models; using Netch.Utils; -using Vanara.PInvoke; -using static Vanara.PInvoke.User32; namespace Netch { @@ -50,15 +47,17 @@ namespace Netch Configuration.Load(); Global.LogStopwatch.Log("Load Configuration"); - // 检查是否已经运行 - if (!Global.Mutex.WaitOne(0, false)) - { - ShowOpened(); - // 退出进程 - Environment.Exit(1); + if (!Global.SingleInstance.IsFirstInstance) + { + Global.SingleInstance.PassArgumentsToFirstInstance(args.Append(Global.ParameterShow)); + Environment.Exit(0); + return; } + Global.SingleInstance.ArgumentsReceived.Subscribe(SingleInstance_ArgumentsReceived); + Global.SingleInstance.ListenForArgumentsFromSuccessiveInstances(); + // 清理上一次的日志文件,防止淤积占用磁盘空间 if (Directory.Exists("logging")) { @@ -106,40 +105,12 @@ namespace Netch Utils.Utils.Open(Logging.LogFile); } - private static void ShowOpened() + private static void SingleInstance_ArgumentsReceived(IEnumerable args) { - HWND GetWindowHandleByPidAndTitle(int process, string title) + if (args.Contains(Global.ParameterShow)) { - var sb = new StringBuilder(256); - HWND pLast = IntPtr.Zero; - do - { - pLast = FindWindowEx(HWND.NULL, pLast, null, null); - GetWindowThreadProcessId(pLast, out var id); - if (id != process) - continue; - - if (GetWindowText(pLast, sb, sb.Capacity) <= 0) - continue; - - if (sb.ToString().Equals(title)) - return pLast; - } while (pLast != IntPtr.Zero); - - return HWND.NULL; + Global.MainForm.ShowMainFormToolStripButton_Click(null!, null!); } - - var self = Process.GetCurrentProcess(); - var activeProcess = Process.GetProcessesByName("Netch").Single(p => p.Id != self.Id); - HWND handle = activeProcess.MainWindowHandle; - if (handle.IsNull) - handle = GetWindowHandleByPidAndTitle(activeProcess.Id, "Netch"); - - if (handle.IsNull) - return; - - ShowWindow(handle, ShowWindowCommand.SW_NORMAL); - SwitchToThisWindow(handle, true); } } } \ No newline at end of file diff --git a/Netch/Netch.csproj b/Netch/Netch.csproj index b9a73a0a..29afc831 100644 --- a/Netch/Netch.csproj +++ b/Netch/Netch.csproj @@ -43,6 +43,7 @@ + diff --git a/Netch/Updater/Updater.cs b/Netch/Updater/Updater.cs index 63628d29..48fe71f2 100644 --- a/Netch/Updater/Updater.cs +++ b/Netch/Updater/Updater.cs @@ -120,7 +120,7 @@ namespace Netch.Updater // save, release mutex, then exit Configuration.Save(); - Global.MainForm.Invoke(new Action(() => { Global.Mutex.ReleaseMutex(); })); + Global.MainForm.Invoke(new Action(() => { Global.SingleInstance.Dispose(); })); Process.Start(Global.NetchExecutable); Global.MainForm.Exit(true, false); }