From abc7ef1e6775dc018253fd6b095297ac90b232fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fele?= Date: Sun, 15 Oct 2023 19:53:12 +0200 Subject: [PATCH] Ensure only a single instance of this app can be run at the same time --- src/ChatPrisma/App.xaml.cs | 19 ++++++++++++++++--- src/ChatPrisma/ChatPrisma.csproj | 1 + src/ChatPrisma/Views/About/AboutViewModel.cs | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/ChatPrisma/App.xaml.cs b/src/ChatPrisma/App.xaml.cs index 4c88657..62aca84 100644 --- a/src/ChatPrisma/App.xaml.cs +++ b/src/ChatPrisma/App.xaml.cs @@ -17,19 +17,25 @@ using Onova; using Onova.Models; using Onova.Services; +using SingleInstanceCore; namespace ChatPrisma; -public partial class App +public partial class App : ISingleInstance { private IHost? _host; - public new static App Current => (App)System.Windows.Application.Current; - private async void App_OnStartup(object sender, StartupEventArgs e) { try { + // Allow only a single instance of this application to run at a time + if (SingleInstance.InitializeAsFirstInstance(this, "Chat Prisma") is false) + { + this.Shutdown(); + return; + } + this._host = this.CreateHostBuilder(e.Args).Build(); await this._host.StartAsync(); @@ -54,6 +60,8 @@ private async void App_OnExit(object sender, ExitEventArgs e) { await this._host.StopAsync(); this._host.Dispose(); + + SingleInstance.Cleanup(); } } @@ -63,6 +71,11 @@ private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandl logger?.LogError(e.Exception, "An unhandled exception occurred."); } + public void OnInstanceInvoked(string[] args) + { + // Right now we don't care about startup args + } + private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Hosting.Host .CreateDefaultBuilder(args) .ConfigureServices((context, services) => diff --git a/src/ChatPrisma/ChatPrisma.csproj b/src/ChatPrisma/ChatPrisma.csproj index 7cc86cd..2f2dd3c 100644 --- a/src/ChatPrisma/ChatPrisma.csproj +++ b/src/ChatPrisma/ChatPrisma.csproj @@ -20,6 +20,7 @@ + diff --git a/src/ChatPrisma/Views/About/AboutViewModel.cs b/src/ChatPrisma/Views/About/AboutViewModel.cs index ac7a1c7..628ab1f 100644 --- a/src/ChatPrisma/Views/About/AboutViewModel.cs +++ b/src/ChatPrisma/Views/About/AboutViewModel.cs @@ -35,6 +35,7 @@ public partial class AboutViewModel(IOptions options) : Obse new ThirdPartyLibrary("Emoji.Wpf", new("https://github.com/samhocevar/emoji.wpf"), "WTFPL", new Uri("https://github.com/samhocevar/emoji.wpf/blob/main/COPYING")), new ThirdPartyLibrary("DevExpress.Mvvm", new("https://github.com/DevExpress/DevExpress.Mvvm.Free"), "MIT", new Uri("https://github.com/DevExpress/DevExpress.Mvvm.Free/blob/main/LICENSE")), new ThirdPartyLibrary("Nerdbank.GitVersioning", new("https://github.com/dotnet/Nerdbank.GitVersioning"), "MIT", new Uri("https://github.com/dotnet/Nerdbank.GitVersioning/blob/main/LICENSE")), + new ThirdPartyLibrary("SingleInstanceCore", new("https://github.com/soheilkd/SingleInstanceCore"), "MIT", new Uri("https://github.com/soheilkd/SingleInstanceCore/blob/master/LICENSE")), }; }