{"record":{"id":"2775f84e4fb87c8e","repo":"AutoDarkMode/Windows-Auto-Night-Mode","slug":"could-not-add-auto-dark-mode-to-autostart","errorCode":null,"errorMessage":"Could not add Auto Dark Mode to autostart","messagePattern":"Could not add Auto Dark Mode to autostart","errorType":"exception","errorClass":"AddAutoStartException","httpStatus":null,"severity":"error","filePath":"AutoDarkModeApp/Utils/Handlers/AutostartHandler.cs","lineNumber":56,"sourceCode":"        catch (Exception ex)\n        {\n            _errorService.ShowErrorMessageFromApi(result, ex, xamlRoot);\n        }\n    }\n\n    public static async void EnableAutoStart(XamlRoot xamlRoot)\n    {\n        ApiResponse result = new()\n        {\n            StatusCode = StatusCode.Err,\n            Message = \"error in frontend: EnableAutostart()\"\n        };\n        try\n        {\n            result = ApiResponse.FromString(await MessageHandler.Client.SendMessageAndGetReplyAsync(Command.AddAutostart));\n            if (result.StatusCode != StatusCode.Ok)\n            {\n                throw new AddAutoStartException($\"Could not add Auto Dark Mode to autostart\", \"AutoCheckBox_Checked\");\n            }\n        }\n        catch (Exception ex)\n        {\n            await _errorService.ShowErrorMessageFromApi(result, ex, xamlRoot);\n        }\n    }\n}\n","sourceCodeStart":38,"sourceCodeEnd":65,"githubUrl":"https://github.com/AutoDarkMode/Windows-Auto-Night-Mode/blob/c15b28e92138a1baff6165bbca80842f06cb819f/AutoDarkModeApp/Utils/Handlers/AutostartHandler.cs#L38-L65","documentation":"Thrown by AutostartHandler.EnableAutoStart when the backend service (AutoDarkModeSvc) returns a non-Ok StatusCode in reply to the AddAutostart IPC command. The frontend sends the command over a named pipe via MessageHandler.Client, parses the reply with ApiResponse.FromString, and throws AddAutoStartException when StatusCode != Ok. Note: AddAutoStartException overrides Message to always return 'Auto start task could not be set.', so the constructor argument is only carried as Source.","triggerScenarios":"EnableAutoStart(xamlRoot) is called (e.g. from a checkbox toggle). MessageHandler.Client.SendMessageAndGetReplyAsync(Command.AddAutostart) succeeds but ApiResponse.FromString(...).StatusCode != 'Ok'. This happens when the service processed the request but failed to register the Windows Task Scheduler autostart task.","commonSituations":"AutoDarkModeSvc is not running or crashed (the default result object with StatusCode.Err is shown); the user lacks privileges to create a scheduled task; a stale/conflicting AutoDarkMode task already exists in Task Scheduler; the service encountered an internal error registering the task and reported Err.","solutions":["Confirm AutoDarkModeSvc is running (Services snap-in or Task Manager); start it if stopped.","Restart the service and retry enabling autostart from Settings.","Open Windows Task Scheduler and check for the AutoDarkMode autostart task — delete a corrupt entry and re-enable.","Run the app elevated if the autostart mode requires administrator privileges.","Inspect the service log in %AppData%\\AutoDarkMode\\logs for the detailed backend error behind the Err status."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Check the service pipe is reachable before attempting autostart ops\npublic static async Task<bool> IsServiceAvailableAsync()\n{\n    try { var r = await MessageHandler.Client.SendMessageAndGetReplyAsync(Command.ValidateAutostart); return r != null; }\n    catch { return false; }\n}","typeGuard":null,"tryCatchPattern":"// EnableAutoStart already wraps in try/catch routing to _errorService.\n// Callers of EnableAutoStart need not add their own catch (it is async-void),\n// but should verify the service is running first:\nif (!await IsServiceAvailableAsync()) { /* inform user, do not call */ return; }","preventionTips":["Confirm AutoDarkModeSvc is running before toggling autostart.","Run the app with sufficient privileges for the chosen autostart mode.","Keep Task Scheduler healthy; clear stale AutoDarkMode tasks before re-enabling.","Check the service log in %AppData%\\AutoDarkMode\\logs whenever StatusCode.Err appears."],"tags":["autostart","ipc","task-scheduler","windows","named-pipe"],"backgroundTag":null,"analyzedSha":"c15b28e92138a1baff6165bbca80842f06cb819f","analyzedAt":"2026-08-13T21:04:56.049Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}