You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows Form中CefSharp多浏览器窗口创建报错解决问询

问题描述

在Windows Form项目中使用CefSharp实现网页浏览功能时,每次打开新的SlaytBrowser窗口,都会在窗口加载事件中执行Cef.Initialize操作,抛出**“CEF can only be initialized once per process”**异常。即使关闭已打开的浏览器窗口,也无法创建新窗口。需求是无论是否关闭已有窗口,都能无错误创建新的CefSharp浏览器窗口。

错误截图

报错详情

以下是JIT调试的替代错误对话框,详情请查看本消息末尾。

************** 异常文本 **************
System.Exception: CEF每个进程只能初始化一次。这是底层CEF/Chromium框架的限制。你可以通过RequestContext.SetPreference在运行时修改许多(并非全部)设置。使用Cef.IsInitialized来避免此异常。如果意外出现此错误,可能是你在创建ChromiumWebBrowser实例后调用了Cef.Initialize,它必须在第一个实例创建之前调用。
在 CefSharp.Core.Cef.Initialize(CefSettingsBase cefSettings, Boolean performDependencyCheck, IApp cefApp) 位置 C:\projects\cefsharp\CefSharp.Core.Runtime\Cef.h:行 260
在 CefSharp.Core.Cef.Initialize(CefSettingsBase cefSettings, Boolean performDependencyCheck, IBrowserProcessHandler browserProcessHandler) 位置 C:\projects\cefsharp\CefSharp.Core.Runtime\Cef.h:行 242
在 CefSharp.Cef.Initialize(CefSettingsBase settings, Boolean performDependencyCheck, IBrowserProcessHandler browserProcessHandler) 位置 C:\projects\cefsharp\CefSharp.Core\Cef.cs:行 146
在 _404_Masaüstü_Uygulaması.SlaytBrowser.SlaytBrowser_Load(Object sender, EventArgs e) 位置 C:\Users\Harun\source\repos\Uc_Haftada_C_Sharp\404 Masaüstü Uygulaması\404 Masaüstü Uygulaması\SlaytBrowser.cs:行 31
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Form.OnCreateControl()
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WmShowWindow(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** 加载的程序集 **************
mscorlib
程序集版本: 4.0.0.0
Win32版本: 4.8.4515.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll

404 Masaüstü Uygulaması
程序集版本: 1.0.0.0
Win32版本: 1.0.0.0
CodeBase: file:///C:/Users/Harun/source/repos/Uc_Haftada_C_Sharp/404%20Masa%C3%BCst%C3%BC%20Uygulamas%C4%B1/404%20Masa%C3%BCst%C3%BC%20Uygulamas%C4%B1/bin/x86/Debug/404%20Masa%C3%BCst%C3%BC%20Uygulamas%C4%B1.exe

System.Windows.Forms
程序集版本: 4.0.0.0
Win32版本: 4.8.4550.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

System
程序集版本: 4.0.0.0
Win32版本: 4.8.4536.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll

System.Drawing
程序集版本: 4.0.0.0
Win32版本: 4.8.4390.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll

System.Configuration
程序集版本: 4.0.0.0
Win32版本: 4.8.4190.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

System.Core
程序集版本: 4.0.0.0
Win32版本: 4.8.4566.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll

System.Xml
程序集版本: 4.0.0.0
Win32版本: 4.8.4084.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll

mscorlib.resources
程序集版本: 4.0.0.0
Win32版本: 4.8.4084.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_tr_b77a5c561934e089/mscorlib.resources.dll

Accessibility
程序集版本: 4.0.0.0
Win32版本: 4.8.4084.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll

CefSharp.WinForms
程序集版本: 106.0.290.0
Win32版本: 106.0.290.0
CodeBase: file:///C:/Users/Harun/source/repos/Uc_Haftada_C_Sharp/404%20Masa%C3%BCst%C3%BC%20Uygulamas%C4%B1/404%20Masa%C3%BCst%C3%BC%20Uygulamas%C4%B1/bin/x86/Debug/CefSharp.WinForms.DLL

CefSharp.Core
程序集版本: 106.0.290.0
Win32版本: 106.0.290.0
CodeBase: file:///C:/Users/Harun/source/repos/Uc_Haftada_C_Sharp/404%20Masa%C3%BCst%C3%BC%20Uygulamas%C4%B1/404%20Masa%C3%BCst%C3%BC%20Uygulamas%C4%B1/bin/x86/Debug/CefSharp.Core.DLL

CefSharp
程序集版本: 106.0.290.0
Win32版本: 106.0.290.0
CodeBase: file:///C:/Users/Harun/source/repos/Uc_Haftada_C_Sharp/404%20Masa%C3%BCst%C3%BC%20Uygulamas%C4%B1/404%20Masa%C3%BCst%C3%BC%20Uygulamas%C4%B1/bin/x86/Debug/CefSharp.DLL

CefSharp.Core.Runtime
程序集版本: 106.0.290.0
Win32版本: 106.0.290
CodeBase: file:///C:/Users/Harun/source/repos/Uc_Haftada_C_Sharp/404%20Masa%C3%BCst%C3%BC%20Uygulamas%C4%B1/404%20Masa%C3%BCst%C3%BC%20Uygulamas%C4%B1/bin/x86/Debug/CefSharp.Core.Runtime.DLL

System.Windows.Forms.resources
程序集版本: 4.0.0.0
Win32版本: 4.8.4084.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_tr_b77a5c561934e089/System.Windows.Forms.resources.dll

************** JIT调试 **************
要启用JIT调试,需在应用程序或计算机的machine.config文件的system.windows.forms节中设置jitDebugging值为true。同时应用程序需在调试模式下编译。

例如:

启用JIT调试后,未处理的异常将被发送到计算机上注册的JIT调试器,而不是在此对话框中处理。

现有代码

创建新SlaytBrowser窗口代码

private void ders6btn_Click(object sender, EventArgs e)
{
    SlaytBrowser slayt_acici = new SlaytBrowser();
    slayt_acici.url = "https://www.google.com";
    slayt_acici.Show();
}

SlaytBrowser窗口代码

private void SlaytBrowser_Load(object sender, EventArgs e)
{
    CefSettings settings = new CefSettings();
    settings.CefCommandLineArgs.Add("disable-gpu", "1");
    Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
    chrome = new ChromiumWebBrowser(url);
    this.panel1.Controls.Add(chrome);
    chrome.Dock = DockStyle.Fill;
    chrome.AddressChanged += Chrome_AddressChanged;
}

private void Chrome_AddressChanged(object sender, AddressChangedEventArgs e)
{
    this.Invoke(new MethodInvoker(() =>
    {
        url = e.Address;
    }));
}

private void button1_Click(object sender, EventArgs e)
{
    chrome.Load(url);
}

解决方案

核心原因

CEF框架限制每个进程只能初始化一次,当前在每个窗口的Load事件中重复调用Cef.Initialize,导致第二次打开窗口时触发异常。

修复步骤

  1. 将CEF初始化移到应用程序启动时
    在项目的Program.cs中,在启动主窗体之前完成CEF的初始化,确保整个进程只执行一次:

    static class Program
    {
        [STAThread]
        static void Main()
        {
            // 初始化CEF
            CefSettings settings = new CefSettings();
            settings.CefCommandLineArgs.Add("disable-gpu", "1");
            if (!Cef.IsInitialized)
            {
                Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
            }
    
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm()); // 你的主窗体
    
            // 应用退出时关闭CEF
            Application.ApplicationExit += (s, e) =>
            {
                if (Cef.IsInitialized)
                {
                    Cef.Shutdown();
                }
            };
        }
    }
    
  2. 修改SlaytBrowser窗口代码,移除重复初始化
    去掉SlaytBrowser_Load中的Cef.Initialize调用,直接创建ChromiumWebBrowser实例:

    private void SlaytBrowser_Load(object sender, EventArgs e)
    {
        chrome = new ChromiumWebBrowser(url);
        this.panel1.Controls.Add(chrome);
        chrome.Dock = DockStyle.Fill;
        chrome.AddressChanged += Chrome_AddressChanged;
    }
    
  3. 确保窗口关闭时正确清理资源
    在SlaytBrowser的FormClosing事件中释放浏览器资源,避免内存泄漏:

    private void SlaytBrowser_FormClosing(object sender, FormClosingEventArgs e)
    {
        chrome?.Dispose();
    }
    

说明

  • Cef.IsInitialized用于检查CEF是否已初始化,避免重复调用Initialize
  • 应用退出时调用Cef.Shutdown,确保CEF资源被正确释放
  • 每个新窗口只需要创建ChromiumWebBrowser实例,无需再次初始化CEF

内容的提问来源于stack exchange,提问作者tanjavaner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 14:50:37