From 4d5152f78a10a5a18f35d4bab36962018a3e820e Mon Sep 17 00:00:00 2001 From: gokcekantarci <115616017+gokcekantarci@users.noreply.github.com> Date: Mon, 5 Jun 2023 13:42:06 +0300 Subject: [PATCH] [Runner]Check for updates and bug report on background thread (#25978) * [Runner] CheckForUpdatesCallback function and ID_REPORT_BUG_COMMAND case in tray_icon moved to threads. * [Runner] Bool flag added to bug report thread. * [Runner] Bool flag added to CheckForUpdatesCallback thread. * [Runner] Review comments added. Uncessary mutex removed. compare_exchange_strong is used for atomic_bool variable checks. --- src/runner/UpdateUtils.cpp | 1 + src/runner/settings_window.cpp | 10 +++++++++- src/runner/tray_icon.cpp | 31 +++++++++++++++++++++---------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/runner/UpdateUtils.cpp b/src/runner/UpdateUtils.cpp index 5de6fa740f..0b182e4a3d 100644 --- a/src/runner/UpdateUtils.cpp +++ b/src/runner/UpdateUtils.cpp @@ -276,6 +276,7 @@ void CheckForUpdatesCallback() } ProcessNewVersionInfo(*new_version_info, state, download_update, false); + UpdateState::store([&](UpdateState& v) { v = std::move(state); }); diff --git a/src/runner/settings_window.cpp b/src/runner/settings_window.cpp index af4e304ab0..b56996aa79 100644 --- a/src/runner/settings_window.cpp +++ b/src/runner/settings_window.cpp @@ -32,6 +32,7 @@ TwoWayPipeMessageIPC* current_settings_ipc = NULL; std::mutex ipc_mutex; std::atomic_bool g_isLaunchInProgress = false; +std::atomic_bool isUpdateCheckThreadRunning = false; json::JsonObject get_power_toys_settings() { @@ -106,7 +107,14 @@ std::optional dispatch_json_action_to_module(const json::JsonObjec } else if (action == L"check_for_updates") { - CheckForUpdatesCallback(); + bool expected_isUpdateCheckThreadRunning = false; + if (isUpdateCheckThreadRunning.compare_exchange_strong(expected_isUpdateCheckThreadRunning,true)) + { + std::thread([]() { + CheckForUpdatesCallback(); + isUpdateCheckThreadRunning.store(false); + }).detach(); + } } else if (action == L"request_update_state_date") { diff --git a/src/runner/tray_icon.cpp b/src/runner/tray_icon.cpp index 60669f7d7c..38d110a6ea 100644 --- a/src/runner/tray_icon.cpp +++ b/src/runner/tray_icon.cpp @@ -43,6 +43,8 @@ struct run_on_main_ui_thread_msg PVOID data; }; +std::atomic_bool isBugReportThreadRunning = false; + bool dispatch_run_on_main_ui_thread(main_loop_callback_function _callback, PVOID data) { if (tray_icon_hwnd == NULL) @@ -96,16 +98,25 @@ void handle_tray_command(HWND window, const WPARAM command_id, LPARAM lparam) { std::wstring bug_report_path = get_module_folderpath(); bug_report_path += L"\\Tools\\PowerToys.BugReportTool.exe"; - SHELLEXECUTEINFOW sei{ sizeof(sei) }; - sei.fMask = { SEE_MASK_FLAG_NO_UI | SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NO_CONSOLE }; - sei.lpFile = bug_report_path.c_str(); - sei.nShow = SW_HIDE; - if (ShellExecuteExW(&sei)) - { - WaitForSingleObject(sei.hProcess, INFINITE); - CloseHandle(sei.hProcess); - static const std::wstring bugreport_success = GET_RESOURCE_STRING(IDS_BUGREPORT_SUCCESS); - MessageBoxW(nullptr, bugreport_success.c_str(), L"PowerToys", MB_OK); + + bool expected_isBugReportThreadRunning = false; + if (isBugReportThreadRunning.compare_exchange_strong(expected_isBugReportThreadRunning, true)) + { + std::thread([bug_report_path]() { + SHELLEXECUTEINFOW sei{ sizeof(sei) }; + sei.fMask = { SEE_MASK_FLAG_NO_UI | SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NO_CONSOLE }; + sei.lpFile = bug_report_path.c_str(); + sei.nShow = SW_HIDE; + if (ShellExecuteExW(&sei)) + { + WaitForSingleObject(sei.hProcess, INFINITE); + CloseHandle(sei.hProcess); + static const std::wstring bugreport_success = GET_RESOURCE_STRING(IDS_BUGREPORT_SUCCESS); + MessageBoxW(nullptr, bugreport_success.c_str(), L"PowerToys", MB_OK); + } + + isBugReportThreadRunning.store(false); + }).detach(); } break;