From 31a5d4924601dee117c53c7aa05f8b231407e0a1 Mon Sep 17 00:00:00 2001 From: Ian O'Neill Date: Wed, 18 Aug 2021 12:20:51 +0100 Subject: [PATCH] Fix race-condition in PowerRename progress UI (#12771) --- src/modules/powerrename/ui/PowerRenameUI.cpp | 12 +++++++++++- src/modules/powerrename/ui/PowerRenameUI.h | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/modules/powerrename/ui/PowerRenameUI.cpp b/src/modules/powerrename/ui/PowerRenameUI.cpp index caf7b3c410..dcbcf18e16 100644 --- a/src/modules/powerrename/ui/PowerRenameUI.cpp +++ b/src/modules/powerrename/ui/PowerRenameUI.cpp @@ -1525,7 +1525,11 @@ HRESULT CPowerRenameProgressUI::Start() m_canceled = false; AddRef(); m_workerThreadHandle = CreateThread(nullptr, 0, s_workerThread, this, 0, nullptr); - if (!m_workerThreadHandle) + if (m_workerThreadHandle) + { + m_loadingThread = true; + } + else { Release(); } @@ -1557,6 +1561,7 @@ DWORD WINAPI CPowerRenameProgressUI::s_workerThread(_In_ void* pv) SetTimer(hwndMessage, TIMERID_CHECKCANCELED, CANCEL_CHECK_INTERVAL, nullptr); sppd->StartProgressDialog(NULL, NULL, PROGDLG_MARQUEEPROGRESS, NULL); } + pThis->m_loadingThread = false; while (pThis->m_sppd && !sppd->HasUserCancelled()) { @@ -1591,6 +1596,11 @@ HRESULT CPowerRenameProgressUI::Stop() void CPowerRenameProgressUI::_Cleanup() { + while (m_loadingThread.load()) + { + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + if (m_sppd) { m_sppd->StopProgressDialog(); diff --git a/src/modules/powerrename/ui/PowerRenameUI.h b/src/modules/powerrename/ui/PowerRenameUI.h index ab036b4db2..caa2acfb10 100644 --- a/src/modules/powerrename/ui/PowerRenameUI.h +++ b/src/modules/powerrename/ui/PowerRenameUI.h @@ -68,6 +68,7 @@ private: long m_refCount = 0; bool m_canceled = false; + std::atomic m_loadingThread{ false }; HANDLE m_workerThreadHandle = nullptr; CComPtr m_sppd; };