ElectronNET/Electron.NET · error · Error

BrowserWindow with id

Error message

BrowserWindow with id '${id}' was not found.

What it means

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.

Solutions

  1. 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.
  2. Re-obtain the window reference from the live Electron.WindowManager.BrowserWindows collection instead of relying on a stored id.
  3. If the window was intentionally closed, treat the id as dead: recreate the window and use the new id for subsequent calls.
  4. Verify the id is a real host-assigned numeric id and that the same host process that created the window is serving this request.
  5. Guard against close/destroy races by checking the window's destroyed/closed state before issuing further window operations.

Example fix

// before
var window = await Electron.WindowManager.CreateWindowAsync();
await window.CloseAsync();
// later, using a stale id
bool focused = await Electron.WindowManager.BrowserWindows
    .First(w => w.Id == staleId).IsFocusedAsync(); // throws 'BrowserWindow with id ... was not found'

// after
var target = Electron.WindowManager.BrowserWindows.FirstOrDefault(w => w.Id == staleId);
bool focused = target != null ? await target.IsFocusedAsync() : false;
Defensive patterns

Strategy: try-catch

Validate before calling

// C#: verify the window is still tracked before calling its API
if (!Electron.WindowManager.BrowserWindows.Any(w => w.Id == id))
{
    // window no longer exists: recreate it or skip the operation
    return;
}

Type guard

// TypeScript-side narrowing used by consumers of getWindowById
function isLiveWindow(win: Electron.BrowserWindow | null | undefined): win is Electron.BrowserWindow {
    return !!win && !win.isDestroyed();
}

Try / catch

// C# wrapper around any id-based window API call
try
{
    bool focused = await Electron.WindowManager.BrowserWindows
        .First(w => w.Id == id).IsFocusedAsync();
}
catch (Exception ex) when (ex.Message.Contains("was not found"))
{
    // window is gone: drop the cached id, recreate or ignore
    cachedWindowIds.Remove(id);
}

Prevention

When it happens

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

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

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of ElectronNET/Electron.NET@87cc6f98b6 (2026-09-14). Data as JSON: /api/errors/3c2f43561a5b70fc. Report an issue: GitHub.

Appendix: source

Thrown at src/ElectronNET.Host/api/browserWindows.ts:929

    getWindowById(id).setBrowserView(browserViewMediateService(browserViewId));
  });

  function getWindowById(id: number): Electron.BrowserWindow {
    const runtimeWindow = BrowserWindow.fromId(id);
    if (runtimeWindow) {
      return runtimeWindow;
    }

    synchronizeWindowRegistry();

    for (let index = 0; index < windows.length; index++) {
      const element = windows[index];
      if (tryGetWindowId(element) === id) {
        return element;
      }
    }

    throw new Error(`BrowserWindow with id '${id}' was not found.`);
  }

  function tryGetWindowId(element: Electron.BrowserWindow): number | null {
    try {
      return element.id;
    } catch {
      return null;
    }
  }

  function synchronizeWindowRegistry(): void {
    const runtimeWindows = BrowserWindow.getAllWindows();
    const runtimeWindowIds = new Set(runtimeWindows.map((entry) => entry.id));

    for (let index = windows.length - 1; index >= 0; index--) {
      const windowId = tryGetWindowId(windows[index]);
      if (windowId === null || !runtimeWindowIds.has(windowId)) {
        windows.splice(index, 1);

View on GitHub (pinned to 87cc6f98b6)