2020-09-21 18:44:16 +08:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
2022-09-14 00:25:19 +08:00
|
|
|
|
using Dispatcher = System.Windows.Threading.Dispatcher;
|
|
|
|
|
|
|
|
|
|
namespace Common.UI
|
2020-09-21 18:44:16 +08:00
|
|
|
|
{
|
|
|
|
|
public static class NativeEventWaiter
|
|
|
|
|
{
|
2022-09-14 00:25:19 +08:00
|
|
|
|
public static void WaitForEventLoop(string eventName, Action callback, Dispatcher dispatcher, CancellationToken cancel)
|
2020-09-21 18:44:16 +08:00
|
|
|
|
{
|
|
|
|
|
new Thread(() =>
|
|
|
|
|
{
|
2022-09-14 00:25:19 +08:00
|
|
|
|
var eventHandle = new EventWaitHandle(false, EventResetMode.ManualReset, eventName);
|
2020-09-21 18:44:16 +08:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
2022-09-12 18:20:24 +08:00
|
|
|
|
if (WaitHandle.WaitAny(new WaitHandle[] { cancel.WaitHandle, eventHandle }) == 1)
|
2020-09-21 18:44:16 +08:00
|
|
|
|
{
|
2022-09-14 00:25:19 +08:00
|
|
|
|
dispatcher.BeginInvoke(callback);
|
2020-09-21 18:44:16 +08:00
|
|
|
|
}
|
2022-09-12 18:20:24 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-21 18:44:16 +08:00
|
|
|
|
}
|
|
|
|
|
}).Start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|