Skip to content

Commit

Permalink
Ensure only a single instance of this app can be run at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
haefele committed Oct 15, 2023
1 parent 7070542 commit abc7ef1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/ChatPrisma/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -54,6 +60,8 @@ private async void App_OnExit(object sender, ExitEventArgs e)
{
await this._host.StopAsync();
this._host.Dispose();

SingleInstance.Cleanup();
}
}

Expand All @@ -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) =>
Expand Down
1 change: 1 addition & 0 deletions src/ChatPrisma/ChatPrisma.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="NLog.Extensions.Hosting" Version="5.3.4" />
<PackageReference Include="FluentIcons.WPF" Version="1.1.220" />
<PackageReference Include="Onova" Version="2.6.10" />
<PackageReference Include="SingleInstanceCore" Version="2.2.2" />
</ItemGroup>

<ItemGroup Label="Versioning">
Expand Down
1 change: 1 addition & 0 deletions src/ChatPrisma/Views/About/AboutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public partial class AboutViewModel(IOptions<ApplicationOptions> 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")),
};
}

Expand Down

0 comments on commit abc7ef1

Please sign in to comment.