Skip to content

Commit

Permalink
Fix for not shutting down on tray 'Exit'
Browse files Browse the repository at this point in the history
  • Loading branch information
hymccord committed May 1, 2022
1 parent b928acf commit d32fe15
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 0 additions & 2 deletions ReadySetTarkov/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ protected override void OnStartup(StartupEventArgs e)

private void RegisterExitEvents()
{
AppDomain.CurrentDomain.ProcessExit += (s, e) => Exiting();
Current.Exit += (s, e) => Exiting();
Current.SessionEnding += (s, e) => Exiting();
}

private void Exiting()
Expand Down
2 changes: 2 additions & 0 deletions ReadySetTarkov/ReadySetTarkovHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using ReadySetTarkov.LogReader;
using ReadySetTarkov.LogReader.Handlers.Application;
using ReadySetTarkov.LogReader.Handlers.Application.LineHandlers;
using ReadySetTarkov.Services;
using ReadySetTarkov.Settings;
using ReadySetTarkov.Tarkov;
using ReadySetTarkov.Utility;
Expand All @@ -30,6 +31,7 @@ public static IHostBuilder AddServices(this IHostBuilder hostBuilder)
_ = services.AddSingleton<ICoreService, Core>();
_ = services.AddSingleton<IHostedService>(s => s.GetRequiredService<ICoreService>());
_ = services.AddHostedService<GameEventHandler>();
_ = services.AddHostedService<ShutdownHandler>();

_ = services.AddSingleton<Game>();
_ = services.AddSingleton<ITarkovGame>(s => s.GetRequiredService<Game>());
Expand Down
33 changes: 33 additions & 0 deletions ReadySetTarkov/Services/ShutdownHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

using Microsoft.Extensions.Hosting;
using Microsoft.VisualStudio.Threading;

namespace ReadySetTarkov.Services;
internal class ShutdownHandler : IHostedService
{
private readonly JoinableTaskFactory _joinableTaskFactory;

public ShutdownHandler(IHostApplicationLifetime hostApplicationLifetime, JoinableTaskFactory joinableTaskFactory)
{
_joinableTaskFactory = joinableTaskFactory;

hostApplicationLifetime.ApplicationStopping.Register(static (c) => _ = (c as ShutdownHandler)!.ShutdownAppAsync(), this);
}

public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;

private async Task ShutdownAppAsync()
{
await _joinableTaskFactory.SwitchToMainThreadAsync();

Application.Current.Shutdown();
}
}

0 comments on commit d32fe15

Please sign in to comment.