From af4dc80ce9dc175d2f120cd51f2b45a12b50b891 Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Mon, 18 Jul 2022 13:43:01 +0200 Subject: [PATCH] properly handle thread when AOT is destroyed (#19489) --- src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp | 13 +++++++------ src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp index b0493c7c8e..aae0ad2303 100644 --- a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp +++ b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp @@ -56,11 +56,12 @@ AlwaysOnTop::~AlwaysOnTop() { if (m_hPinEvent) { - SetEvent(m_hPinEvent); - m_thread.join(); CloseHandle(m_hPinEvent); } + m_running = false; + m_thread.join(); + CleanUp(); } @@ -274,12 +275,12 @@ void AlwaysOnTop::RegisterLLKH() Logger::warn(L"Failed to create pinEvent. {}", get_last_error_or_default(GetLastError())); return; } - + m_thread = std::thread([this]() { MSG msg; - while (1) + while (m_running) { - DWORD dwEvt = MsgWaitForMultipleObjects(1, &m_hPinEvent, false, INFINITE, QS_ALLINPUT); + DWORD dwEvt = MsgWaitForMultipleObjects(1, &m_hPinEvent, false, 0, QS_ALLINPUT); switch (dwEvt) { case WAIT_OBJECT_0: @@ -296,7 +297,7 @@ void AlwaysOnTop::RegisterLLKH() } break; default: - return false; + break; } } }); diff --git a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h index 663aae65ba..91a18cc5af 100644 --- a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h +++ b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h @@ -50,6 +50,7 @@ private: HANDLE m_hPinEvent; std::thread m_thread; const bool m_useCentralizedLLKH; + bool m_running = true; LRESULT WndProc(HWND, UINT, WPARAM, LPARAM) noexcept; void HandleWinHookEvent(WinHookEvent* data) noexcept;