From 29d482e89292eace81f48acc1d5fda6b8a3bb02f Mon Sep 17 00:00:00 2001 From: Casper Sparre Date: Wed, 18 Feb 2026 22:30:38 +0100 Subject: [PATCH] Added check if other process is already running. --- Cajetan.OF-DL/ProgramCajetan.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Cajetan.OF-DL/ProgramCajetan.cs b/Cajetan.OF-DL/ProgramCajetan.cs index 88b5277..4602ce5 100644 --- a/Cajetan.OF-DL/ProgramCajetan.cs +++ b/Cajetan.OF-DL/ProgramCajetan.cs @@ -1,3 +1,5 @@ +using System.Diagnostics; +using System.Reflection; using Microsoft.Extensions.DependencyInjection; AnsiConsole.Write(new FigletText("Welcome to OF-DL").Color(Color.Red)); @@ -5,6 +7,8 @@ AnsiConsole.Write(new FigletText("Welcome to OF-DL").Color(Color.Red)); ServiceCollection services = await ConfigureServices(args); ServiceProvider serviceProvider = services.BuildServiceProvider(); +ExitIfOtherProcess(serviceProvider); + Worker worker = serviceProvider.GetRequiredService(); await worker.RunAsync(); @@ -149,3 +153,23 @@ static bool ParseCommandlineArgs(string[] args, Config currentConfig, out Cajeta } } +static void ExitIfOtherProcess(ServiceProvider serviceProvider) +{ + 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.."); + + serviceProvider.GetRequiredService().ExitWithCode(0); +} +