Migrate to HMBSbige.SingleInstance

This commit is contained in:
ChsBuffer
2021-03-24 11:31:25 +08:00
parent 32d3e97288
commit 5d5ee40cd6
5 changed files with 24 additions and 46 deletions

View File

@@ -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;

View File

@@ -29,9 +29,7 @@ namespace Netch
/// </summary>
private static readonly Lazy<MainForm> LazyMainForm = new(() => new MainForm());
private static readonly Lazy<Mutex> 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";
}
}

View File

@@ -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<string> 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);
}
}
}

View File

@@ -43,6 +43,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="HMBSbige.SingleInstance" Version="5.0.0" />
<PackageReference Include="MaxMind.GeoIP2" Version="4.0.1" />
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="2.0.66" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />

View File

@@ -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);
}