From 3d2a5fd282183cc9ce76a31341aa7a0a5ca10e05 Mon Sep 17 00:00:00 2001 From: Casper Sparre Date: Sat, 8 Mar 2025 14:51:07 +0100 Subject: [PATCH] Added exiting if other process is detected, to avoid overlapping runs --- OF DL/Program.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/OF DL/Program.cs b/OF DL/Program.cs index db94d0f..5e8b590 100644 --- a/OF DL/Program.cs +++ b/OF DL/Program.cs @@ -22,6 +22,7 @@ using static OF_DL.Entities.Messages.Messages; using Akka.Configuration; using System.Text; using static Akka.Actor.ProviderSelection; +using System.Diagnostics; namespace OF_DL; @@ -118,6 +119,8 @@ public class Program AnsiConsole.Write(new FigletText("Welcome to OF-DL").Color(Color.Red)); + ExitIfOtherProcess(); + //Remove config.json and convert to config.conf if (File.Exists("config.json")) { @@ -3256,4 +3259,24 @@ public class Program return Enum.Parse("_" + value, ignoreCase: true); } + + static void ExitIfOtherProcess() + { + Assembly entryAssembly = Assembly.GetEntryAssembly(); + AssemblyName entryAssemblyName = entryAssembly?.GetName(); + + if (entryAssemblyName?.Name is null) + return; + + Process thisProcess = Process.GetCurrentProcess(); + Process[] otherProcesses = [.. Process.GetProcessesByName(entryAssemblyName.Name).Where(p => p.Id != thisProcess.Id)]; + + if (otherProcesses.Length <= 0) + return; + + AnsiConsole.Markup($"[green]Other OF DL process detected, exiting..\n[/]"); + Log.Warning("Other OF DL process detected, exiting.."); + + Environment.Exit(0); + } }