2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="SingleAppInstance.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2020-03-17 02:45:19 +13:00
|
|
|
|
|
2020-03-28 12:42:32 +13:00
|
|
|
|
namespace SystemTrayMenu.Utilities
|
2020-03-17 02:45:19 +13:00
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
2022-01-02 07:10:20 +13:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using SystemTrayMenu.UserInterface.HotkeyTextboxControl;
|
|
|
|
|
using WindowsInput;
|
|
|
|
|
using WindowsInput.Native;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2020-03-17 02:45:19 +13:00
|
|
|
|
internal static class SingleAppInstance
|
|
|
|
|
{
|
2022-01-02 07:10:20 +13:00
|
|
|
|
internal static bool Initialize(bool killOtherInstances)
|
2020-03-17 02:45:19 +13:00
|
|
|
|
{
|
2022-01-02 07:10:20 +13:00
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
|
|
try
|
2020-03-17 02:45:19 +13:00
|
|
|
|
{
|
2022-01-02 07:10:20 +13:00
|
|
|
|
foreach (Process p in Process.GetProcessesByName(
|
|
|
|
|
Process.GetCurrentProcess().ProcessName).
|
|
|
|
|
Where(s => s.Id != Process.GetCurrentProcess().Id))
|
2020-03-17 02:45:19 +13:00
|
|
|
|
{
|
2022-01-02 07:10:20 +13:00
|
|
|
|
if (!killOtherInstances)
|
|
|
|
|
{
|
|
|
|
|
Keys modifiers = HotkeyControl.HotkeyModifiersFromString(Properties.Settings.Default.HotKey);
|
|
|
|
|
Keys hotkey = HotkeyControl.HotkeyFromString(Properties.Settings.Default.HotKey);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
VirtualKeyCode virtualKeyCodeModifiers = (VirtualKeyCode)Enum.Parse(
|
|
|
|
|
typeof(VirtualKeyCode), modifiers.ToString().ToUpperInvariant());
|
|
|
|
|
VirtualKeyCode virtualKeyCodeHotkey = (VirtualKeyCode)Enum.Parse(
|
|
|
|
|
typeof(VirtualKeyCode), hotkey.ToString().ToUpperInvariant());
|
|
|
|
|
|
|
|
|
|
new InputSimulator().Keyboard.ModifiedKeyStroke(virtualKeyCodeModifiers, virtualKeyCodeHotkey);
|
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
|
|
// how to solve with several modifier keys?
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn("Send hoktey to other instance failed", ex);
|
|
|
|
|
killOtherInstances = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-17 02:45:19 +13:00
|
|
|
|
|
2022-01-02 07:10:20 +13:00
|
|
|
|
if (killOtherInstances)
|
2020-03-17 02:45:19 +13:00
|
|
|
|
{
|
2022-01-02 07:10:20 +13:00
|
|
|
|
try
|
2020-03-17 02:45:19 +13:00
|
|
|
|
{
|
2020-06-29 19:16:23 +12:00
|
|
|
|
if (!p.CloseMainWindow())
|
|
|
|
|
{
|
|
|
|
|
p.Kill();
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-03-17 02:45:19 +13:00
|
|
|
|
p.WaitForExit();
|
2020-06-29 19:16:23 +12:00
|
|
|
|
p.Close();
|
2020-03-17 02:45:19 +13:00
|
|
|
|
}
|
2022-01-02 07:10:20 +13:00
|
|
|
|
catch (Exception ex)
|
2020-03-30 06:26:45 +13:00
|
|
|
|
{
|
|
|
|
|
Log.Error("Run as single instance failed", ex);
|
2022-01-02 07:10:20 +13:00
|
|
|
|
success = false;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
2020-03-17 02:45:19 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-02 07:10:20 +13:00
|
|
|
|
catch (Exception ex)
|
2020-03-17 02:45:19 +13:00
|
|
|
|
{
|
2022-01-02 07:10:20 +13:00
|
|
|
|
Log.Error("Run as single instance failed", ex);
|
2020-03-17 02:45:19 +13:00
|
|
|
|
}
|
2022-01-02 07:10:20 +13:00
|
|
|
|
|
|
|
|
|
return success;
|
2020-03-17 02:45:19 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|