Repository: microsoft/PowerToys
File: src/runner/settings_window.cpp (C++)

Code before:
                    {
                        schedule_restart_as_elevated(true);
                        PostQuitMessage(0);
                    }
                }
                else if (action == L"restart_maintain_elevation")
                {
                    // this was added to restart and maintain elevation, which is needed after settings are change from outside the normal process.
                    // since a normal PostQuitMessage(0) would usually cause this process to save it's in memory settings to disk, we need to
                    // send a PostQuitMessage(1) and check for that on exit, and skip the settings-flush.
                    auto loaded = PTSettingsHelper::load_general_settings();

                    if (is_process_elevated())
                    {
                        schedule_restart_as_elevated(true);
                        PostQuitMessage(1);
                    }
                    else
                    {
                        schedule_restart_as_non_elevated(true);
                        PostQuitMessage(1);
                    }
                }
                else if (action == L"check_for_updates")
                {
                    if (!isUpdateCheckThreadRunning)
                    {
                        isUpdateCheckThreadRunning = true;
                        std::thread([]() {
                            CheckForUpdatesCallback();
                            isUpdateCheckThreadRunning = false;
                        }).detach();
                    }
                }
                else if (action == L"request_update_state_date")
                {
                    json::JsonObject json;

                    auto update_state = UpdateState::read();
                    if (update_state.githubUpdateLastCheckedDate)
                    {
                        const time_t date = *update_state.githubUpdateLastCheckedDate;
                        json.SetNamedValue(L"updateStateDate", json::value(std::to_wstring(date)));
                    }

                    result.emplace(json.Stringify());
                }
            }
            catch (...)
            {
            }

Review comment (bug):
Maybe we could use [compare_exchange_strong](https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange) in these cases, even though the probability of the bug is really small.

Code after:
                    {
                        schedule_restart_as_elevated(true);
                        PostQuitMessage(0);
                    }
                }
                else if (action == L"restart_maintain_elevation")
                {
                    // this was added to restart and maintain elevation, which is needed after settings are change from outside the normal process.
                    // since a normal PostQuitMessage(0) would usually cause this process to save it's in memory settings to disk, we need to
                    // send a PostQuitMessage(1) and check for that on exit, and skip the settings-flush.
                    auto loaded = PTSettingsHelper::load_general_settings();

                    if (is_process_elevated())
                    {
                        schedule_restart_as_elevated(true);
                        PostQuitMessage(1);
                    }
                    else
                    {
                        schedule_restart_as_non_elevated(true);
                        PostQuitMessage(1);
                    }
                }
                else if (action == L"check_for_updates")
                {
                    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")
                {
                    json::JsonObject json;

                    auto update_state = UpdateState::read();
                    if (update_state.githubUpdateLastCheckedDate)
                    {
                        const time_t date = *update_state.githubUpdateLastCheckedDate;
                        json.SetNamedValue(L"updateStateDate", json::value(std::to_wstring(date)));
                    }

                    result.emplace(json.Stringify());
                }
            }
            catch (...)
            {
            }