[PTRun] Gracefully shutdown all threads when exiting (#20450)

This commit is contained in:
Andrey Nekrasov
2022-09-12 13:20:24 +03:00
committed by GitHub
parent 73590c3ea9
commit 38e7b3b7ae
4 changed files with 31 additions and 26 deletions

View File

@@ -12,18 +12,22 @@ namespace PowerLauncher.Helper
{
public static class NativeEventWaiter
{
public static void WaitForEventLoop(string eventName, Action callback)
public static void WaitForEventLoop(string eventName, Action callback, CancellationToken cancel)
{
new Thread(() =>
{
var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);
while (true)
{
if (eventHandle.WaitOne())
if (WaitHandle.WaitAny(new WaitHandle[] { cancel.WaitHandle, eventHandle }) == 1)
{
Log.Info($"Successfully waited for {eventName}", MethodBase.GetCurrentMethod().DeclaringType);
Application.Current.Dispatcher.Invoke(callback);
}
else
{
return;
}
}
}).Start();
}