mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-26 02:28:17 +08:00
30 lines
685 B
C++
30 lines
685 B
C++
#include "pch.h"
|
|
#include "native_event_waiter.h"
|
|
|
|
void NativeEventWaiter::run()
|
|
{
|
|
while (!aborting)
|
|
{
|
|
auto result = WaitForSingleObject(event_handle, timeout);
|
|
if (!aborting && result == WAIT_OBJECT_0)
|
|
{
|
|
action();
|
|
}
|
|
}
|
|
}
|
|
|
|
NativeEventWaiter::NativeEventWaiter(const std::wstring& event_name, std::function<void()> action)
|
|
{
|
|
event_handle = CreateEventW(NULL, FALSE, FALSE, event_name.c_str());
|
|
this->action = action;
|
|
running_thread = std::thread([&]() { run(); });
|
|
}
|
|
|
|
NativeEventWaiter::~NativeEventWaiter()
|
|
{
|
|
aborting = true;
|
|
SetEvent(event_handle);
|
|
running_thread.join();
|
|
CloseHandle(event_handle);
|
|
}
|