diff --git a/src/RepoM.App/App.xaml.cs b/src/RepoM.App/App.xaml.cs index 0d113996..5552e1d4 100644 --- a/src/RepoM.App/App.xaml.cs +++ b/src/RepoM.App/App.xaml.cs @@ -13,6 +13,7 @@ namespace RepoM.App; using RepoM.App.i18n; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; +using Microsoft.VisualStudio.Services.WebApi; using RepoM.Api.Plugins; using RepoM.App.Plugins; using Serilog; @@ -28,6 +29,7 @@ namespace RepoM.App; /// public partial class App : Application { + private static Mutex? _mutex; private static IRepositoryMonitor? _repositoryMonitor; private TaskbarIcon? _notifyIcon; private ModuleService? _moduleService; @@ -37,6 +39,11 @@ public partial class App : Application [STAThread] public static void Main() { + if (IsAlreadyRunning()) + { + return; + } + Thread.CurrentThread.Name ??= "UI"; var app = new App(); app.InitializeComponent(); @@ -109,7 +116,9 @@ protected override void OnExit(ExitEventArgs e) // #pragma warning disable CA1416 // Validate platform compatibility _notifyIcon?.Dispose(); -// #pragma warning restore CA1416 // Validate platform compatibility + // #pragma warning restore CA1416 // Validate platform compatibility + + ReleaseAndDisposeMutex(); base.OnExit(e); } @@ -149,5 +158,49 @@ private static void UseRepositoryMonitor(Container container) _repositoryMonitor.Observe(); } + private static bool IsAlreadyRunning() + { + bool createdNew; + + try + { + _mutex = new Mutex(true, "Local\\github.com/coenm/RepoM", out createdNew); + } + catch (Exception) + { + return true; + } + + if (!createdNew) + { + _mutex.Dispose(); + _mutex = null; + return true; + } + + return false; + } + + private static void ReleaseAndDisposeMutex() + { + try + { + _mutex?.ReleaseMutex(); + } + catch (Exception) + { + // ignore + } + + try + { + _mutex?.Dispose(); + } + catch (Exception) + { + // ignore + } + } + public static string? AvailableUpdate { get; private set; } = null; } \ No newline at end of file