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

Code before:
    {
        openOobe = !PTSettingsHelper::get_oobe_opened_state();
    }
    catch (const std::exception& e)
    {
        Logger::error("Failed to get or save OOBE state with an exception: {}", e.what());
    }

    bool openScoobe = false;
    try
    {
        std::wstring last_version_run = PTSettingsHelper::get_last_version_run();
        openScoobe = last_version_run != get_product_version();
    }
    catch (const std::exception& e)
    {
        Logger::error("Failed to get last version with an exception: {}", e.what());
    }

    int result = 0;
    try
    {
        // Singletons initialization order needs to be preserved, first events and
        // then modules to guarantee the reverse destruction order.
        modules();
        cleanup_updates();

        auto general_settings = load_general_settings();

        // Apply the general settings but don't save it as the modules() variable has not been loaded yet
        apply_general_settings(general_settings, false);
        int rvalue = 0;
        const bool elevated = is_process_elevated();
        const bool with_dont_elevate_arg = cmdLine.find("--dont-elevate") != std::string::npos;
        const bool run_elevated_setting = general_settings.GetNamedBoolean(L"run_elevated", false);

        if (elevated && with_dont_elevate_arg && !run_elevated_setting)
        {
            Logger::info("Scheduling restart as non elevated");
            schedule_restart_as_non_elevated();
            result = 0;
        }
        else if (elevated || !run_elevated_setting || with_dont_elevate_arg)
        {
            result = runner(elevated, open_settings, settings_window, openOobe, openScoobe);

            // Save settings on closing
            auto general_settings = get_general_settings();
            PTSettingsHelper::save_general_settings(general_settings.to_json());
        }
        else

Review comment (suggestion):
perhaps we should move it into a separate thread, e.g. move before

```cpp
        std::thread{ [] {
            PeriodicUpdateWorker();
        } }.detach();
```

Code after:
    {
        openOobe = !PTSettingsHelper::get_oobe_opened_state();
    }
    catch (const std::exception& e)
    {
        Logger::error("Failed to get or save OOBE state with an exception: {}", e.what());
    }

    bool openScoobe = false;
    try
    {
        std::wstring last_version_run = PTSettingsHelper::get_last_version_run();
        openScoobe = last_version_run != get_product_version();
    }
    catch (const std::exception& e)
    {
        Logger::error("Failed to get last version with an exception: {}", e.what());
    }

    int result = 0;
    try
    {
        // Singletons initialization order needs to be preserved, first events and
        // then modules to guarantee the reverse destruction order.
        modules();

        std::thread{ [] {
            cleanup_updates();
        } }.detach();

        auto general_settings = load_general_settings();

        // Apply the general settings but don't save it as the modules() variable has not been loaded yet
        apply_general_settings(general_settings, false);
        int rvalue = 0;
        const bool elevated = is_process_elevated();
        const bool with_dont_elevate_arg = cmdLine.find("--dont-elevate") != std::string::npos;
        const bool run_elevated_setting = general_settings.GetNamedBoolean(L"run_elevated", false);

        if (elevated && with_dont_elevate_arg && !run_elevated_setting)
        {
            Logger::info("Scheduling restart as non elevated");
            schedule_restart_as_non_elevated();
            result = 0;
        }
        else if (elevated || !run_elevated_setting || with_dont_elevate_arg)
        {
            result = runner(elevated, open_settings, settings_window, openOobe, openScoobe);

            // Save settings on closing
            auto general_settings = get_general_settings();