AutoDarkMode/Windows-Auto-Night-Mode · error · AddAutoStartException

Could not add Auto Dark Mode to autostart

Error message

Could not add Auto Dark Mode to autostart

What it means

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.

Source

Thrown at AutoDarkModeApp/Utils/Handlers/AutostartHandler.cs:56

        catch (Exception ex)
        {
            _errorService.ShowErrorMessageFromApi(result, ex, xamlRoot);
        }
    }

    public static async void EnableAutoStart(XamlRoot xamlRoot)
    {
        ApiResponse result = new()
        {
            StatusCode = StatusCode.Err,
            Message = "error in frontend: EnableAutostart()"
        };
        try
        {
            result = ApiResponse.FromString(await MessageHandler.Client.SendMessageAndGetReplyAsync(Command.AddAutostart));
            if (result.StatusCode != StatusCode.Ok)
            {
                throw new AddAutoStartException($"Could not add Auto Dark Mode to autostart", "AutoCheckBox_Checked");
            }
        }
        catch (Exception ex)
        {
            await _errorService.ShowErrorMessageFromApi(result, ex, xamlRoot);
        }
    }
}

View on GitHub (pinned to c15b28e921)

Solutions

  1. Confirm AutoDarkModeSvc is running (Services snap-in or Task Manager); start it if stopped.
  2. Restart the service and retry enabling autostart from Settings.
  3. Open Windows Task Scheduler and check for the AutoDarkMode autostart task — delete a corrupt entry and re-enable.
  4. Run the app elevated if the autostart mode requires administrator privileges.
  5. Inspect the service log in %AppData%\AutoDarkMode\logs for the detailed backend error behind the Err status.
Defensive patterns

Strategy: try-catch

Validate before calling

// Check the service pipe is reachable before attempting autostart ops
public static async Task<bool> IsServiceAvailableAsync()
{
    try { var r = await MessageHandler.Client.SendMessageAndGetReplyAsync(Command.ValidateAutostart); return r != null; }
    catch { return false; }
}

Try / catch

// EnableAutoStart already wraps in try/catch routing to _errorService.
// Callers of EnableAutoStart need not add their own catch (it is async-void),
// but should verify the service is running first:
if (!await IsServiceAvailableAsync()) { /* inform user, do not call */ return; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of AutoDarkMode/Windows-Auto-Night-Mode@c15b28e921 (2026-08-13). Data as JSON: /api/errors/2775f84e4fb87c8e. Report an issue: GitHub.