From f42ea82466fd5f32e6214cc052bee7a8cb9f60bc Mon Sep 17 00:00:00 2001 From: Marco van den Oever Date: Sat, 23 Nov 2024 14:18:15 +0100 Subject: [PATCH] [WinForms] Hotkey service exit --- .../Wtq.Services.WinForms/Native/HotkeyManager.cs | 6 +++++- .../Wtq.Services.WinForms/WinFormsHotkeyService.cs | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/20-Services/Wtq.Services.WinForms/Native/HotkeyManager.cs b/src/20-Services/Wtq.Services.WinForms/Native/HotkeyManager.cs index c4eaab98..bb85a43d 100644 --- a/src/20-Services/Wtq.Services.WinForms/Native/HotkeyManager.cs +++ b/src/20-Services/Wtq.Services.WinForms/Native/HotkeyManager.cs @@ -17,7 +17,6 @@ internal static class HotkeyManager static HotkeyManager() { - // TODO: Thread doesn't exit properly. Thread messageLoop = new(delegate () { Application.Run(new MessageWindow()); @@ -39,6 +38,11 @@ static HotkeyManager() /// public static event EventHandler HotkeyPressed = (sender, e) => { }; + public static void Exit() + { + Application.Exit(); + } + public static int RegisterHotkey(Keys key, KeyModifiers modifiers) { _windowReadyEvent.WaitOne(); diff --git a/src/20-Services/Wtq.Services.WinForms/WinFormsHotkeyService.cs b/src/20-Services/Wtq.Services.WinForms/WinFormsHotkeyService.cs index 60ec7a95..610b5402 100644 --- a/src/20-Services/Wtq.Services.WinForms/WinFormsHotkeyService.cs +++ b/src/20-Services/Wtq.Services.WinForms/WinFormsHotkeyService.cs @@ -2,7 +2,8 @@ namespace Wtq.Services.WinForms; -public class WinFormsHotkeyService : IHostedService +public sealed class WinFormsHotkeyService + : IDisposable, IHostedService { private readonly ILogger _log = Log.For(); @@ -33,6 +34,11 @@ public WinFormsHotkeyService(IWtqBus bus) }; } + public void Dispose() + { + HotkeyManager.Exit(); + } + public Task StartAsync(CancellationToken cancellationToken) { // Only here to make sure an instance of this class is created.