cefsharp/CefSharp · error · Exception

An instance of the underlying offscreen browser has already

Error message

An instance of the underlying offscreen browser has already been created, this method can only be called once.

What it means

Thrown by ChromiumWebBrowser.CreateBrowser when the underlying offscreen browser has already been created (browserCreated flag is true). CEF only allows one native browser instance per ChromiumWebBrowser, so a second CreateBrowser call is rejected. The flag is set immediately before actual creation to prevent re-entrancy.

Source

Thrown at CefSharp.OffScreen/ChromiumWebBrowser.cs:355

                var renderHandler = RenderHandler;
                RenderHandler = null;
                renderHandler?.Dispose();
            }

            Cef.RemoveDisposable(this);
        }

        /// <summary>
        /// Create the underlying browser. The instance address, browser settings and request context will be used.
        /// </summary>
        /// <param name="windowInfo">Window information used when creating the browser</param>
        /// <param name="browserSettings">Browser initialization settings</param>
        /// <exception cref="System.Exception">An instance of the underlying offscreen browser has already been created, this method can only be called once.</exception>
        public void CreateBrowser(IWindowInfo windowInfo = null, IBrowserSettings browserSettings = null)
        {
            if (browserCreated)
            {
                throw new Exception("An instance of the underlying offscreen browser has already been created, this method can only be called once.");
            }

            browserCreated = true;

            if (browserSettings == null)
            {
                browserSettings = Core.ObjectFactory.CreateBrowserSettings(autoDispose: true);
            }

            if (windowInfo == null)
            {
                windowInfo = Core.ObjectFactory.CreateWindowInfo();
                windowInfo.SetAsWindowless(IntPtr.Zero);
            }

            //TODO: We need some sort of timeout and
            //if we use the same approach for WPF/WinForms then
            //we need to move the common code into the partial class

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Check IsBrowserInitialized / browserCreated before calling CreateBrowser.
  2. Avoid both an auto-initializing constructor and an explicit CreateBrowser — pick one strategy.
  3. If you need a fresh browser, dispose the old ChromiumWebBrowser and create a new instance.

Example fix

// before
browser.CreateBrowser();

// after
if (!browser.IsBrowserInitialized && !browserCreated)
    browser.CreateBrowser();
Defensive patterns

Strategy: validation

Validate before calling

if (browser.IsBrowserInitialized || browserCreated) return;
browser.CreateBrowser();

Type guard

public static bool CanCreateBrowser(ChromiumWebBrowser b) => !b.IsBrowserInitialized;

Try / catch

try { browser.CreateBrowser(windowInfo, settings); }
catch (Exception ex) when (ex.Message.Contains("already been created")) { /* already initialized, ok */ }

Prevention

When it happens

Trigger: Calling CreateBrowser() twice. Calling CreateBrowser() after the implicit creation triggered by the constructor or by accessing a property that lazily initializes the browser.

Common situations: Constructor with an initial URL auto-creates the browser; a later explicit CreateBrowser call collides. Calling CreateBrowser in a Loaded event that also runs because of autoInit. Reusing a disposed ChromiumWebBrowser instance.

Related errors


AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13). Data as JSON: /api/errors/2507323664efc55e. Report an issue: GitHub.