[General]Use background threads in NativeEventWaiters to prevent dangling threads (#35637)

* Use Tasks in NativeEventWaiters to prevent dangling threads

* Use background threads instead
This commit is contained in:
Stefan Markovic 2024-10-29 16:31:13 +01:00 committed by GitHub
parent b204353308
commit 72a481b17c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 7 deletions

View File

@ -14,7 +14,7 @@ namespace Hosts.Helpers
public static void WaitForEventLoop(string eventName, Action callback) public static void WaitForEventLoop(string eventName, Action callback)
{ {
var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
new Thread(() => var t = new Thread(() =>
{ {
var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName); var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);
while (true) while (true)
@ -24,7 +24,10 @@ namespace Hosts.Helpers
dispatcherQueue.TryEnqueue(() => callback()); dispatcherQueue.TryEnqueue(() => callback());
} }
} }
}).Start(); });
t.IsBackground = true;
t.Start();
} }
} }
} }

View File

@ -14,7 +14,7 @@ namespace Peek.UI.Native
public static void WaitForEventLoop(string eventName, Action callback) public static void WaitForEventLoop(string eventName, Action callback)
{ {
var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
new Thread(() => var t = new Thread(() =>
{ {
var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName); var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);
while (true) while (true)
@ -24,7 +24,10 @@ namespace Peek.UI.Native
dispatcherQueue.TryEnqueue(() => callback()); dispatcherQueue.TryEnqueue(() => callback());
} }
} }
}).Start(); });
t.IsBackground = true;
t.Start();
} }
} }
} }

View File

@ -4,7 +4,7 @@
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Microsoft.UI.Dispatching; using Microsoft.UI.Dispatching;
namespace Microsoft.PowerToys.Settings.UI.Helpers namespace Microsoft.PowerToys.Settings.UI.Helpers
@ -14,7 +14,7 @@ namespace Microsoft.PowerToys.Settings.UI.Helpers
public static void WaitForEventLoop(string eventName, Action callback) public static void WaitForEventLoop(string eventName, Action callback)
{ {
var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
new Task(() => var t = new Thread(() =>
{ {
var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName); var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);
while (true) while (true)
@ -24,7 +24,10 @@ namespace Microsoft.PowerToys.Settings.UI.Helpers
dispatcherQueue.TryEnqueue(() => callback()); dispatcherQueue.TryEnqueue(() => callback());
} }
} }
}).Start(); });
t.IsBackground = true;
t.Start();
} }
} }
} }