{"record":{"id":"cdbe8912be6f24cb","repo":"NickeManarin/ScreenToGif","slug":"it-was-not-possible-to-get-a-list-of-known-screens-cdbe89","errorCode":null,"errorMessage":"It was not possible to get a list of known screens.","messagePattern":"It was not possible to get a list of known screens\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"warning","filePath":"ScreenToGif/Windows/Other/DownloadDialog.xaml.cs","lineNumber":267,"sourceCode":"    }\n\n    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)\n    {\n        Global.UpdateAvailable.TaskCompletionSource = null;\n    }\n\n\n    private void CenterOnScreen()\n    {\n        //Since the list of monitors could have been changed, it needs to be queried again.\n        var monitors = MonitorHelper.AllMonitorsGranular();\n\n        //Detect closest screen to the point (previously selected top/left point or current mouse coordinate).\n        var point = new Point((int)Left, (int)Top);\n        var closest = monitors.FirstOrDefault(x => x.Bounds.Contains(point)) ?? monitors.FirstOrDefault(x => x.IsPrimary) ?? monitors.FirstOrDefault();\n\n        if (closest == null)\n            throw new Exception(\"It was not possible to get a list of known screens.\");\n\n        //Move the window to the correct location.\n        Left = closest.WorkingArea.Left + closest.WorkingArea.Width / 2d - ActualWidth / 2d;\n        Top = closest.WorkingArea.Top + closest.WorkingArea.Height / 2d - ActualHeight / 2d;\n    }\n}","sourceCodeStart":249,"sourceCodeEnd":273,"githubUrl":"https://github.com/NickeManarin/ScreenToGif/blob/a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd/ScreenToGif/Windows/Other/DownloadDialog.xaml.cs#L249-L273","documentation":"Identical root cause to error 41 but thrown inside DownloadDialog.CenterOnScreen(). MonitorHelper.AllMonitorsGranular() returns an empty list, so the FirstOrDefault chain (point-containing, primary, any) resolves to null and the generic Exception fires. This dialog centers itself on the closest monitor before showing, and cannot do so without an enumerated screen.","triggerScenarios":"CenterOnScreen() is called when the DownloadDialog is being positioned/shown. AllMonitorsGranular() returns empty because User32.EnumDisplayMonitors enumerated no displays — the same native-empty condition as error 41 but triggered from a different window's layout path.","commonSituations":"Monitors disconnected/locked while a download was triggered; the dialog opens during a display-settings-change transition; running from a non-interactive or RDP-disconnected session; display driver crashed leaving no GDI desktop during the dialog open.","solutions":["Ensure a display is connected and active before triggering the download dialog.","Reconnect displays and reopen the dialog so CenterOnScreen re-enumerates.","Catch the exception in CenterOnScreen and skip centering (let WPF place the window) instead of crashing the download flow.","Guard: if the monitor list is empty, fall back to SystemParameters.WorkArea for centering."],"exampleFix":"// before\nvar monitors = MonitorHelper.AllMonitorsGranular();\nvar point = new Point((int)Left, (int)Top);\nvar closest = monitors.FirstOrDefault(x => x.Bounds.Contains(point)) ?? monitors.FirstOrDefault(x => x.IsPrimary) ?? monitors.FirstOrDefault();\nif (closest == null)\n    throw new Exception(\"It was not possible to get a list of known screens.\");\nLeft = closest.WorkingArea.Left + closest.WorkingArea.Width / 2d - ActualWidth / 2d;\nTop = closest.WorkingArea.Top + closest.WorkingArea.Height / 2d - ActualHeight / 2d;\n\n// after: fall back to primary working area, never throw on layout\nvar monitors = MonitorHelper.AllMonitorsGranular();\nvar closest = monitors.FirstOrDefault(x => x.Bounds.Contains(new Point((int)Left, (int)Top))\n           ?? monitors.FirstOrDefault(x => x.IsPrimary)\n           ?? monitors.FirstOrDefault();\n\nvar area = closest?.WorkingArea ?? SystemParameters.WorkArea;\nLeft = area.Left + area.Width / 2d - ActualWidth / 2d;\nTop = area.Top + area.Height / 2d - ActualHeight / 2d;","handlingStrategy":"fallback","validationCode":"// Validate enumeration before centering the dialog.\nvar monitors = MonitorHelper.AllMonitorsGranular();\nif (monitors.Count == 0)\n{\n    // Fall back to the WPF primary working area; do not throw.\n    var wa = SystemParameters.WorkArea;\n    Left = wa.Left + wa.Width / 2d - ActualWidth / 2d;\n    Top = wa.Top + wa.Height / 2d - ActualHeight / 2d;\n    return;\n}","typeGuard":"static Rect ResolveCenterArea(List<Monitor> monitors, Point point)\n{\n    var closest = monitors.FirstOrDefault(x => x.Bounds.Contains(point))\n               ?? monitors.FirstOrDefault(x => x.IsPrimary)\n               ?? monitors.FirstOrDefault();\n    return closest?.WorkingArea ?? SystemParameters.WorkArea;\n}","tryCatchPattern":"try\n{\n    CenterOnScreen();\n}\ncatch (Exception e) when (e.Message.Contains(\"list of known screens\"))\n{\n    // Center on the WPF primary work area; the download must still proceed.\n    LogWriter.Log(e, \"Monitor enumeration failed in CenterOnScreen; using SystemParameters.WorkArea.\");\n    var wa = SystemParameters.WorkArea;\n    Left = wa.Left + wa.Width / 2d - ActualWidth / 2d;\n    Top = wa.Top + wa.Height / 2d - ActualHeight / 2d;\n}","preventionTips":["Layout/centering code should never throw — always degrade to SystemParameters.","Retry AllMonitorsGranular() once before giving up, since display changes are transient.","Defer CenterOnScreen until after the DisplaySettingsChanged event settles.","Treat monitor enumeration as best-effort in dialog windows, not a hard prerequisite."],"tags":["monitor","display-enumeration","enumdisplaymonitors","window-layout","download-dialog"],"backgroundTag":null,"analyzedSha":"a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd","analyzedAt":"2026-08-13T11:12:06.147Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}