{"record":{"id":"c57db822cbccdf8c","repo":"AutoDarkMode/Windows-Auto-Night-Mode","slug":"theme-switching-is-unsuccessful-couldn-t-clean-up","errorCode":null,"errorMessage":"Theme switching is unsuccessful: Couldn't clean up monitor list, {result}","messagePattern":"Theme switching is unsuccessful: Couldn't clean up monitor list, (.+?)","errorType":"exception","errorClass":"SwitchThemeException","httpStatus":null,"severity":"warning","filePath":"AutoDarkModeApp/Views/WallpaperPickerPage.xaml.cs","lineNumber":111,"sourceCode":"            if (result != StatusCode.Ok)\n            {\n                throw new SwitchThemeException(result, \"WallpaperPickerPage\");\n            }\n        }\n        catch (Exception ex)\n        {\n            await _errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, \"WallpaperPickerPage\");\n        }\n    }\n\n    private async void RemoveDisconnectedMonitorsHyperlinkButton_Click(object sender, RoutedEventArgs e)\n    {\n        try\n        {\n            string result = await MessageHandler.Client.SendMessageAndGetReplyAsync(Command.CleanMonitors);\n            if (result != StatusCode.Ok)\n            {\n                throw new SwitchThemeException($\"Couldn't clean up monitor list, {result}\", \"WallpaperPickerPage\");\n            }\n            try\n            {\n                _builder.Load();\n                List<MonitorSettings> monitors = _builder.Config.WallpaperSwitch.Component.Monitors;\n                MonitorsComboBox.ItemsSource = monitors;\n                MonitorsComboBox.SelectedItem = monitors.FirstOrDefault();\n            }\n            catch (Exception ex)\n            {\n                await _errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, \"CleanMonitors\");\n            }\n        }\n        catch (Exception ex)\n        {\n            await _errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, \"CleanMonitors\");\n        }\n    }","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/AutoDarkMode/Windows-Auto-Night-Mode/blob/c15b28e92138a1baff6165bbca80842f06cb819f/AutoDarkModeApp/Views/WallpaperPickerPage.xaml.cs#L93-L129","documentation":"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().","triggerScenarios":"User clicks the 'remove disconnected monitors' hyperlink. SendMessageAndGetReplyAsync(Command.CleanMonitors) returns != 'Ok'. The throw at line 111 is caught by the outer catch.","commonSituations":"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.","solutions":["Re-run DetectMonitors to refresh state, then retry cleanup.","Ensure the service is running and check its log.","Manually reconcile the monitor list in config if cleanup keeps failing.","Use a more appropriate exception type (not SwitchThemeException) so the message is not prefixed with 'Theme switching is unsuccessful'."],"exampleFix":"// before — reuses SwitchThemeException producing a misleading double prefix\nif (result != StatusCode.Ok)\n    throw new SwitchThemeException($\"Couldn't clean up monitor list, {result}\", \"WallpaperPickerPage\");\n\n// after — use a domain-appropriate exception\nif (result != StatusCode.Ok)\n    throw new InvalidOperationException($\"Couldn't clean up monitor list: {result}\");","handlingStrategy":"try-catch","validationCode":"// Re-detect monitors before cleanup so the list is current\npublic static async Task<bool> RefreshedBeforeCleanAsync()\n{\n    try { var r = await MessageHandler.Client.SendMessageAndGetReplyAsync(Command.DetectMonitors); return r == StatusCode.Ok; }\n    catch { return false; }\n}","typeGuard":null,"tryCatchPattern":"// RemoveDisconnectedMonitorsHyperlinkButton_Click already try/catches.\n// Replace SwitchThemeException with a clearer type so the message is not\n// prefixed with 'Theme switching is unsuccessful':\ncatch (InvalidOperationException ex) { await _errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, \"CleanMonitors\"); }","preventionTips":["Use a domain-appropriate exception for monitor cleanup, not SwitchThemeException.","Refresh monitor state (DetectMonitors) before offering cleanup.","Handle the inner _builder.Load() failure separately from the CleanMonitors failure.","Check the service log when cleanup repeatedly fails."],"tags":["monitor-cleanup","ipc","named-pipe","wallpaper","misused-exception"],"backgroundTag":null,"analyzedSha":"c15b28e92138a1baff6165bbca80842f06cb819f","analyzedAt":"2026-08-13T21:04:56.049Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}