diff --git a/src/runner/main.cpp b/src/runner/main.cpp index c1abf7d333..49ed9cc4fd 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "update_state.h" #include "update_utils.h" @@ -252,10 +253,36 @@ void RequestExplorerRestart() if (MessageBoxW(nullptr, localized_strings::PT_UPDATE_MESSAGE_BOX_TEXT, L"PowerToys", - MB_ICONINFORMATION | MB_YESNO | MB_DEFBUTTON1) == IDYES) + MB_ICONINFORMATION | MB_YESNO | MB_DEFBUTTON1) != IDYES) { - RestartProcess(EXPLORER_PROCESS_NAME); + return; } + std::thread{ [] { + RestartProcess(EXPLORER_PROCESS_NAME); + + constexpr size_t max_checks = 10; + for (size_t i = 0; i < max_checks; ++i) + { + Sleep(1000); + const bool explorerStarted = !getProcessHandlesByName(L"explorer.exe", {}).empty(); + if (explorerStarted) + { + return; + } + } + + // Timeout - restart explorer manually + SHELLEXECUTEINFOW sei{ sizeof(sei) }; + sei.fMask = { SEE_MASK_NOASYNC | SEE_MASK_FLAG_NO_UI | SEE_MASK_NO_CONSOLE | SEE_MASK_NOCLOSEPROCESS }; + sei.lpFile = L"cmd"; + sei.lpParameters = L"/c explorer.exe"; + sei.nShow = SW_HIDE; + ShellExecuteExW(&sei); + // Let cmd launch explorer, then terminate it + Sleep(1000); + TerminateProcess(sei.hProcess, 0); + CloseHandle(sei.hProcess); + } }.detach(); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)