{"record":{"id":"6ef613191cc88ca9","repo":"AutoDarkMode/Windows-Auto-Night-Mode","slug":"auto-start-task-could-not-be-set","errorCode":null,"errorMessage":"Auto start task could not be set.","messagePattern":"Auto start task could not be set\\.","errorType":"exception","errorClass":"AddAutoStartException","httpStatus":null,"severity":"error","filePath":"AutoDarkModeApp/ViewModels/SettingsViewModel.cs","lineNumber":135,"sourceCode":"        {\n            _updater.CheckNewVersion();\n            _dispatcherQueue.TryEnqueue(() =>\n            {\n                UpdatesDate = _updater.UpdateAvailable() ? \"Msg_UpdateAvailable\".GetLocalized() : \"Msg_NoUpdate\".GetLocalized();\n            });\n        });\n    }\n\n    [RelayCommand]\n    private async Task AutostartRefresh()\n    {\n        SetAutostartDetailsVisibility(false);\n        try\n        {\n            var response = ApiResponse.FromString(await MessageHandler.Client.SendMessageAndGetReplyAsync($\"{Command.ValidateAutostart} true\", 2));\n            if (response.StatusCode == StatusCode.Err)\n            {\n                throw new AddAutoStartException();\n            }\n            await GetAutostartInfo();\n        }\n        catch (Exception)\n        {\n            throw new AddAutoStartException($\"Could not validate autostart\", \"ValidateAutostart\");\n        }\n        await Task.Delay(fakeResponsiveUIDelay);\n        SetAutostartDetailsVisibility(true);\n    }\n\n    public SettingsViewModel(IErrorService errorService, ILocalSettingsService localSettingsService)\n    {\n        _dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();\n        _updater = new();\n        _errorService = errorService;\n        _localSettingsService = localSettingsService;\n","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/AutoDarkMode/Windows-Auto-Night-Mode/blob/c15b28e92138a1baff6165bbca80842f06cb819f/AutoDarkModeApp/ViewModels/SettingsViewModel.cs#L117-L153","documentation":"Thrown by the parameterless AddAutoStartException constructor in SettingsViewModel.AutostartRefresh when the service replies to the 'ValidateAutostart true' command with StatusCode.Err. The parameterless constructor's overridden Message always reads 'Auto start task could not be set.' ValidateAutostart asks the service to verify (and optionally repair) the autostart task registration.","triggerScenarios":"AutostartRefresh [RelayCommand] sends $\"{Command.ValidateAutostart} true\" with a 2-second timeout. ApiResponse.FromString(...).StatusCode == 'Err'. The AddAutoStartException() is thrown at line 135 and then caught by the catch-all at line 139 which re-wraps it.","commonSituations":"The autostart task is missing or corrupt in Task Scheduler; the service cannot validate the task because of privilege issues; the 2-second timeout is too short on a slow/busy machine so the service returns Err; the service is not running.","solutions":["Ensure AutoDarkModeSvc is running and responsive before triggering AutostartRefresh.","Increase the short 2-second reply timeout if the validation is slow under load.","Recreate the autostart task: disable then re-enable autostart in Settings.","Check Task Scheduler for the AutoDarkMode task and its last run result."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Validate the service reply timeout is adequate before calling ValidateAutostart\npublic static async Task<bool> CanValidateAutostartAsync(int timeoutSeconds)\n{\n    try\n    {\n        var r = ApiResponse.FromString(await MessageHandler.Client.SendMessageAndGetReplyAsync($\"{Command.ValidateAutostart} true\", timeoutSeconds));\n        return r.StatusCode != StatusCode.Err;\n    }\n    catch { return false; }\n}","typeGuard":null,"tryCatchPattern":"// AutostartRefresh is a [RelayCommand]; the AddAutoStartException propagates to\n// the command infrastructure. Wrap the command body or subscribe to errors:\ntry { await AutostartRefresh(); }\ncatch (AddAutoStartException) { /* notify user, keep UI consistent */ }","preventionTips":["Ensure the service is responsive before triggering validation.","Use a timeout longer than 2 seconds on slow or busy machines.","Pre-check Task Scheduler for a valid AutoDarkMode task.","Treat a validation failure as recoverable: offer a re-create action in the UI."],"tags":["autostart","ipc","task-scheduler","validation","named-pipe"],"backgroundTag":null,"analyzedSha":"c15b28e92138a1baff6165bbca80842f06cb819f","analyzedAt":"2026-08-13T21:04:56.049Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}