/ src / modules / ShortcutGuide / ShortcutGuide / native_event_waiter.cpp
native_event_waiter.cpp
 1  #include "pch.h"
 2  #include "native_event_waiter.h"
 3  
 4  void NativeEventWaiter::run()
 5  {
 6      while (!aborting)
 7      {
 8          auto result = WaitForSingleObject(event_handle, timeout);
 9          if (!aborting && result == WAIT_OBJECT_0)
10          {
11              action();
12          }
13      }
14  }
15  
16  NativeEventWaiter::NativeEventWaiter(const std::wstring& event_name, std::function<void()> action)
17  {
18      event_handle = CreateEventW(NULL, FALSE, FALSE, event_name.c_str());
19      this->action = action;
20      running_thread = std::thread([&]() { run(); });
21  }
22  
23  NativeEventWaiter::~NativeEventWaiter()
24  {
25      aborting = true;
26      SetEvent(event_handle);
27      running_thread.join();
28      CloseHandle(event_handle);
29  }