mirror of
https://github.com/Hofknecht/SystemTrayMenu.git
synced 2024-09-30 00:56:47 +13:00
55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
using Clearcove.Logging;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
|
|
namespace SystemTrayMenu.Helper
|
|
{
|
|
internal static class SingleAppInstance
|
|
{
|
|
internal static void Initialize()
|
|
{
|
|
Logger log = new Logger(nameof(SingleAppInstance));
|
|
|
|
if (IsAnyOtherInstancesofAppAlreadyRunning())
|
|
{
|
|
KillOtherInstancesOfApp();
|
|
bool KillOtherInstancesOfApp()
|
|
{
|
|
bool killedAProcess = false;
|
|
int ownPID = Process.GetCurrentProcess().Id;
|
|
|
|
try
|
|
{
|
|
foreach (Process p in Process.GetProcessesByName(
|
|
Process.GetCurrentProcess().ProcessName).
|
|
Where(p => p.Id != ownPID))
|
|
{
|
|
p.Kill();
|
|
p.WaitForExit();
|
|
killedAProcess = true;
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
log.Warn($"{exception.ToString()}");
|
|
}
|
|
|
|
return killedAProcess;
|
|
}
|
|
}
|
|
bool IsAnyOtherInstancesofAppAlreadyRunning()
|
|
{
|
|
//string pid = Process.Id.ToString();
|
|
foreach (Process p in Process.GetProcessesByName(
|
|
Process.GetCurrentProcess().ProcessName).
|
|
Where(s => s.Id != Process.GetCurrentProcess().Id))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|