{"record":{"id":"3c2f43561a5b70fc","repo":"ElectronNET/Electron.NET","slug":"browserwindow-with-id-id-was-not-found-browserwindows","errorCode":null,"errorMessage":"BrowserWindow with id '${id}' was not found.","messagePattern":"BrowserWindow with id '(.+?)' was not found\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/ElectronNET.Host/api/browserWindows.ts","lineNumber":929,"sourceCode":"    getWindowById(id).setBrowserView(browserViewMediateService(browserViewId));\n  });\n\n  function getWindowById(id: number): Electron.BrowserWindow {\n    const runtimeWindow = BrowserWindow.fromId(id);\n    if (runtimeWindow) {\n      return runtimeWindow;\n    }\n\n    synchronizeWindowRegistry();\n\n    for (let index = 0; index < windows.length; index++) {\n      const element = windows[index];\n      if (tryGetWindowId(element) === id) {\n        return element;\n      }\n    }\n\n    throw new Error(`BrowserWindow with id '${id}' was not found.`);\n  }\n\n  function tryGetWindowId(element: Electron.BrowserWindow): number | null {\n    try {\n      return element.id;\n    } catch {\n      return null;\n    }\n  }\n\n  function synchronizeWindowRegistry(): void {\n    const runtimeWindows = BrowserWindow.getAllWindows();\n    const runtimeWindowIds = new Set(runtimeWindows.map((entry) => entry.id));\n\n    for (let index = windows.length - 1; index >= 0; index--) {\n      const windowId = tryGetWindowId(windows[index]);\n      if (windowId === null || !runtimeWindowIds.has(windowId)) {\n        windows.splice(index, 1);","sourceCodeStart":911,"sourceCodeEnd":947,"githubUrl":"https://github.com/ElectronNET/Electron.NET/blob/87cc6f98b6a9c5968d7846c0e775ea713bd0d7b2/src/ElectronNET.Host/api/browserWindows.ts#L911-L947","documentation":"This error is thrown by getWindowById in Electron.NET's host (src/Electron.NET.Host/api/browserWindows.ts) when no managed BrowserWindow whose id matches the given id exists in the host's windows collection. Electron.NET's ASP.NET-side APIs (Electron.WindowManager and window helpers like isFocused, isDestroyed, isVisible, isModal, isMaximized) resolve a C# window reference to a host-side BrowserWindow by numeric id. If the id is stale, wrong, or the window was already closed and pruned from the collection, the lookup fails and this Error is thrown.","triggerScenarios":"Calling any id-based window API (window, isFocused, isDestroyed, isVisible, isModal, isMaximized) with an id of a window that was already closed and removed from the host's windows list; passing a hardcoded or stale window id after app restarts, hot reload, or window recreation; a race where the window closes asynchronously while an API call referencing its id is still in flight; passing an id that was never registered because window creation failed or the id came from a different host instance.","commonSituations":"Developers store a window id in a C# variable or database and later operate on it after the user closed the window; multi-window apps where windows are created/destroyed dynamically and a cached id is not refreshed; background services calling window APIs after the window was disposed; dev-loop scenarios (hot reload) where host state was reset but old ids are reused.","solutions":["Verify the window still exists before using its id: check Electron.WindowManager.BrowserWindows for the id, and track window close events to prune stale ids.","Re-obtain the window reference from the live Electron.WindowManager.BrowserWindows collection instead of relying on a stored id.","If the window was intentionally closed, treat the id as dead: recreate the window and use the new id for subsequent calls.","Verify the id is a real host-assigned numeric id and that the same host process that created the window is serving this request.","Guard against close/destroy races by checking the window's destroyed/closed state before issuing further window operations."],"exampleFix":"// before\nvar window = await Electron.WindowManager.CreateWindowAsync();\nawait window.CloseAsync();\n// later, using a stale id\nbool focused = await Electron.WindowManager.BrowserWindows\n    .First(w => w.Id == staleId).IsFocusedAsync(); // throws 'BrowserWindow with id ... was not found'\n\n// after\nvar target = Electron.WindowManager.BrowserWindows.FirstOrDefault(w => w.Id == staleId);\nbool focused = target != null ? await target.IsFocusedAsync() : false;","handlingStrategy":"try-catch","validationCode":"// C#: verify the window is still tracked before calling its API\nif (!Electron.WindowManager.BrowserWindows.Any(w => w.Id == id))\n{\n    // window no longer exists: recreate it or skip the operation\n    return;\n}","typeGuard":"// TypeScript-side narrowing used by consumers of getWindowById\nfunction isLiveWindow(win: Electron.BrowserWindow | null | undefined): win is Electron.BrowserWindow {\n    return !!win && !win.isDestroyed();\n}","tryCatchPattern":"// C# wrapper around any id-based window API call\ntry\n{\n    bool focused = await Electron.WindowManager.BrowserWindows\n        .First(w => w.Id == id).IsFocusedAsync();\n}\ncatch (Exception ex) when (ex.Message.Contains(\"was not found\"))\n{\n    // window is gone: drop the cached id, recreate or ignore\n    cachedWindowIds.Remove(id);\n}","preventionTips":["Never cache BrowserWindow ids across close/recreate cycles; fetch them from Electron.WindowManager.BrowserWindows at use time.","Subscribe to window close events and prune cached ids when a window closes.","Check window liveness (IsDestroyed / isDestroyed) before issuing further window API calls.","Wrap id-based window API calls in try/catch so a closed window degrades gracefully instead of crashing the request.","In multi-window apps, keep a map from your own logical window name to the live BrowserWindow object rather than raw ids."],"tags":["electron","window-management","stale-reference","resource-not-found"],"backgroundTag":"resource-not-found","analyzedSha":"87cc6f98b6a9c5968d7846c0e775ea713bd0d7b2","analyzedAt":"2026-09-14T03:09:47.608Z","contentChangedAt":"2026-09-14T03:09:47.608Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}