/ src / modules / cmdpal / Core / Microsoft.CmdPal.Core.Common / Helpers / NativeEventWaiter.cs
NativeEventWaiter.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Threading;
 7  
 8  using Microsoft.UI.Dispatching;
 9  
10  namespace Microsoft.CmdPal.Core.Common.Helpers;
11  
12  public static partial class NativeEventWaiter
13  {
14      public static void WaitForEventLoop(string eventName, Action callback)
15      {
16          var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
17          var t = new Thread(() =>
18          {
19              var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);
20              while (true)
21              {
22                  if (eventHandle.WaitOne())
23                  {
24                      dispatcherQueue.TryEnqueue(() => callback());
25                  }
26              }
27          });
28  
29          t.IsBackground = true;
30          t.Start();
31      }
32  }