2023-05-02 02:24:33 +12:00
|
|
|
|
// <copyright file="SingleAppInstance.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.Utilities
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
using System.Linq;
|
2023-05-26 10:34:31 +12:00
|
|
|
|
using SystemTrayMenu.Helpers;
|
2023-05-02 02:24:33 +12:00
|
|
|
|
|
|
|
|
|
internal static class SingleAppInstance
|
|
|
|
|
{
|
|
|
|
|
internal static bool Initialize()
|
|
|
|
|
{
|
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (Process p in Process.GetProcessesByName(
|
|
|
|
|
Process.GetCurrentProcess().ProcessName).
|
|
|
|
|
Where(s => s.Id != Environment.ProcessId))
|
|
|
|
|
{
|
|
|
|
|
if (Properties.Settings.Default.SendHotkeyInsteadKillOtherInstances)
|
2022-10-23 11:02:31 +13:00
|
|
|
|
{
|
2023-05-23 09:57:54 +12:00
|
|
|
|
string hotKeyString = Properties.Settings.Default.HotKey;
|
2023-06-01 10:32:28 +12:00
|
|
|
|
var (modifiers, key) = GlobalHotkeys.ModifiersAndKeyFromInvariantString(hotKeyString);
|
2023-05-02 02:24:33 +12:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-22 07:13:18 +12:00
|
|
|
|
#if TODO // HOTKEY - Maybe replace with sockets or pipes?
|
|
|
|
|
// E.g. https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-use-named-pipes-for-network-interprocess-communication
|
2023-05-02 02:24:33 +12:00
|
|
|
|
List<VirtualKeyCode> virtualKeyCodesModifiers = new();
|
|
|
|
|
foreach (string key in modifiers.ToString().ToUpperInvariant().Split(", "))
|
|
|
|
|
{
|
|
|
|
|
if (key == "NONE")
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VirtualKeyCode virtualKeyCode = VirtualKeyCode.LWIN;
|
|
|
|
|
virtualKeyCode = key switch
|
|
|
|
|
{
|
|
|
|
|
"ALT" => VirtualKeyCode.MENU,
|
|
|
|
|
_ => (VirtualKeyCode)Enum.Parse(
|
|
|
|
|
typeof(VirtualKeyCode), key.ToUpperInvariant()),
|
|
|
|
|
};
|
|
|
|
|
virtualKeyCodesModifiers.Add(virtualKeyCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VirtualKeyCode virtualKeyCodeHotkey = 0;
|
2023-06-01 10:32:28 +12:00
|
|
|
|
if (Enum.IsDefined(typeof(VirtualKeyCode), (int)key))
|
2023-05-02 02:24:33 +12:00
|
|
|
|
{
|
2023-06-01 10:32:28 +12:00
|
|
|
|
virtualKeyCodeHotkey = (VirtualKeyCode)(int)key;
|
2023-05-02 02:24:33 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new InputSimulator().Keyboard.ModifiedKeyStroke(virtualKeyCodesModifiers, virtualKeyCodeHotkey);
|
|
|
|
|
|
|
|
|
|
success = false;
|
2023-05-22 07:13:18 +12:00
|
|
|
|
#endif
|
2022-10-23 11:02:31 +13:00
|
|
|
|
}
|
2023-05-02 02:24:33 +12:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-05-23 09:57:54 +12:00
|
|
|
|
Log.Warn($"Send hoktey {hotKeyString} to other instance failed", ex);
|
2023-05-02 02:24:33 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-22 07:13:18 +12:00
|
|
|
|
else
|
2023-05-02 02:24:33 +12:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!p.CloseMainWindow())
|
|
|
|
|
{
|
|
|
|
|
p.Kill();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.WaitForExit();
|
|
|
|
|
p.Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("Run as single instance failed", ex);
|
|
|
|
|
success = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("Run as single instance failed", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|