AutoDarkMode/Windows-Auto-Night-Mode · warning · SwitchThemeException

Theme switching is unsuccessful: Couldn't clean up monitor l

Error message

Theme switching is unsuccessful: Couldn't clean up monitor list, {result}

What it means

Thrown in WallpaperPickerPage.RemoveDisconnectedMonitorsHyperlinkButton_Click when the reply to Command.CleanMonitors is not Ok. SwitchThemeException is reused with an explicit message 'Couldn't clean up monitor list, {result}', producing 'Theme switching is unsuccessful: Couldn't clean up monitor list, {result}'. The double prefix is misleading — CleanMonitors is unrelated to theme switching. Caught locally; an inner try/catch also guards _builder.Load().

Source

Thrown at AutoDarkModeApp/Views/WallpaperPickerPage.xaml.cs:111

            if (result != StatusCode.Ok)
            {
                throw new SwitchThemeException(result, "WallpaperPickerPage");
            }
        }
        catch (Exception ex)
        {
            await _errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "WallpaperPickerPage");
        }
    }

    private async void RemoveDisconnectedMonitorsHyperlinkButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            string result = await MessageHandler.Client.SendMessageAndGetReplyAsync(Command.CleanMonitors);
            if (result != StatusCode.Ok)
            {
                throw new SwitchThemeException($"Couldn't clean up monitor list, {result}", "WallpaperPickerPage");
            }
            try
            {
                _builder.Load();
                List<MonitorSettings> monitors = _builder.Config.WallpaperSwitch.Component.Monitors;
                MonitorsComboBox.ItemsSource = monitors;
                MonitorsComboBox.SelectedItem = monitors.FirstOrDefault();
            }
            catch (Exception ex)
            {
                await _errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "CleanMonitors");
            }
        }
        catch (Exception ex)
        {
            await _errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "CleanMonitors");
        }
    }

View on GitHub (pinned to c15b28e921)

Solutions

  1. Re-run DetectMonitors to refresh state, then retry cleanup.
  2. Ensure the service is running and check its log.
  3. Manually reconcile the monitor list in config if cleanup keeps failing.
  4. Use a more appropriate exception type (not SwitchThemeException) so the message is not prefixed with 'Theme switching is unsuccessful'.

Example fix

// before — reuses SwitchThemeException producing a misleading double prefix
if (result != StatusCode.Ok)
    throw new SwitchThemeException($"Couldn't clean up monitor list, {result}", "WallpaperPickerPage");

// after — use a domain-appropriate exception
if (result != StatusCode.Ok)
    throw new InvalidOperationException($"Couldn't clean up monitor list: {result}");
Defensive patterns

Strategy: try-catch

Validate before calling

// Re-detect monitors before cleanup so the list is current
public static async Task<bool> RefreshedBeforeCleanAsync()
{
    try { var r = await MessageHandler.Client.SendMessageAndGetReplyAsync(Command.DetectMonitors); return r == StatusCode.Ok; }
    catch { return false; }
}

Try / catch

// RemoveDisconnectedMonitorsHyperlinkButton_Click already try/catches.
// Replace SwitchThemeException with a clearer type so the message is not
// prefixed with 'Theme switching is unsuccessful':
catch (InvalidOperationException ex) { await _errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "CleanMonitors"); }

Prevention

When it happens

Trigger: User clicks the 'remove disconnected monitors' hyperlink. SendMessageAndGetReplyAsync(Command.CleanMonitors) returns != 'Ok'. The throw at line 111 is caught by the outer catch.

Common situations: Service cannot prune the monitor list (e.g. monitors reconnected between detection and cleanup); service returned Err; the clean operation partially failed; service not running.

Related errors


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