From 72a481b17c25fe73a671219bba91ccb0041cd540 Mon Sep 17 00:00:00 2001 From: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:31:13 +0100 Subject: [PATCH] [General]Use background threads in NativeEventWaiters to prevent dangling threads (#35637) * Use Tasks in NativeEventWaiters to prevent dangling threads * Use background threads instead --- src/modules/Hosts/Hosts/Helpers/NativeEventWaiter.cs | 7 +++++-- src/modules/peek/Peek.UI/NativeEventWaiter.cs | 7 +++++-- src/settings-ui/Settings.UI/Helpers/NativeEventWaiter.cs | 9 ++++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/modules/Hosts/Hosts/Helpers/NativeEventWaiter.cs b/src/modules/Hosts/Hosts/Helpers/NativeEventWaiter.cs index b63468d553..9737cdae04 100644 --- a/src/modules/Hosts/Hosts/Helpers/NativeEventWaiter.cs +++ b/src/modules/Hosts/Hosts/Helpers/NativeEventWaiter.cs @@ -14,7 +14,7 @@ namespace Hosts.Helpers public static void WaitForEventLoop(string eventName, Action callback) { var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); - new Thread(() => + var t = new Thread(() => { var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName); while (true) @@ -24,7 +24,10 @@ namespace Hosts.Helpers dispatcherQueue.TryEnqueue(() => callback()); } } - }).Start(); + }); + + t.IsBackground = true; + t.Start(); } } } diff --git a/src/modules/peek/Peek.UI/NativeEventWaiter.cs b/src/modules/peek/Peek.UI/NativeEventWaiter.cs index e35ad98cbb..c49d8b0800 100644 --- a/src/modules/peek/Peek.UI/NativeEventWaiter.cs +++ b/src/modules/peek/Peek.UI/NativeEventWaiter.cs @@ -14,7 +14,7 @@ namespace Peek.UI.Native public static void WaitForEventLoop(string eventName, Action callback) { var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); - new Thread(() => + var t = new Thread(() => { var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName); while (true) @@ -24,7 +24,10 @@ namespace Peek.UI.Native dispatcherQueue.TryEnqueue(() => callback()); } } - }).Start(); + }); + + t.IsBackground = true; + t.Start(); } } } diff --git a/src/settings-ui/Settings.UI/Helpers/NativeEventWaiter.cs b/src/settings-ui/Settings.UI/Helpers/NativeEventWaiter.cs index b8094cd57f..de6707dd38 100644 --- a/src/settings-ui/Settings.UI/Helpers/NativeEventWaiter.cs +++ b/src/settings-ui/Settings.UI/Helpers/NativeEventWaiter.cs @@ -4,7 +4,7 @@ using System; using System.Threading; -using System.Threading.Tasks; + using Microsoft.UI.Dispatching; namespace Microsoft.PowerToys.Settings.UI.Helpers @@ -14,7 +14,7 @@ namespace Microsoft.PowerToys.Settings.UI.Helpers public static void WaitForEventLoop(string eventName, Action callback) { var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); - new Task(() => + var t = new Thread(() => { var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName); while (true) @@ -24,7 +24,10 @@ namespace Microsoft.PowerToys.Settings.UI.Helpers dispatcherQueue.TryEnqueue(() => callback()); } } - }).Start(); + }); + + t.IsBackground = true; + t.Start(); } } }