CefWebBrowser doesn't show up

10,923

Solution 1

I too had problems launching the browser. I could load all the CEF DLL's, but the browser wouldn't show up! All I got was the spinning wait mouse cursor when hovering above the control.

Unfortunately I haven't found the root of the problem, but since the sample project CefGlue.Client works, I simply copied it to my solution instead.

Also, I don't see how you are initializing the CEF runtime. Have a look in Program.cs in CefGlue.Client how it's done, but it's basically this:

    [STAThread]
    private static int Main(string[] args)
    {
        try
        {
            CefRuntime.Load();
        }
        catch (DllNotFoundException ex)
        {
            MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return 1;
        }
        catch (CefRuntimeException ex)
        {
            MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return 2;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return 3;
        }

        var mainArgs = new CefMainArgs(args);
        var app = new DemoApp();

        var exitCode = CefRuntime.ExecuteProcess(mainArgs, app);
        if (exitCode != -1)
            return exitCode;

        var settings = new CefSettings
            {
                // BrowserSubprocessPath = @"D:\fddima\Projects\Xilium\Xilium.CefGlue\CefGlue.Demo\bin\Release\Xilium.CefGlue.Demo.exe",
                SingleProcess = false,
                MultiThreadedMessageLoop = true,
                LogSeverity = CefLogSeverity.Disable,
                LogFile = "CefGlue.log",
            };

        CefRuntime.Initialize(mainArgs, settings, app);

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        if (!settings.MultiThreadedMessageLoop)
        {
            Application.Idle += (sender, e) => { CefRuntime.DoMessageLoopWork(); };
        }

        Application.Run(new MainForm());

        CefRuntime.Shutdown();
        return 0;
    }

Solution 2

You shouldn't use SingleProcess for production, you can actually leave the multiprocess mode (SingleProcess=false) but need to disable Visual Studio Hosting Process and you wont have problem for debugging (spinning wait cursor)

Solution 3

I also had the same problem,I solved it by modify "BrowserSubprocessPath",the defualt is like this:

        //var browserProcessPath = CombinePaths(localFolder, "..", "..", "..",
        //    "CefGlue.Demo.WinForms", "bin", "Release", "Xilium.CefGlue.Demo.WinForms.exe");

        var browserProcessPath = CombinePaths(localFolder, "Xilium.CefGlue.Demo.WinForms.exe");

        var settings = new CefSettings
        {
            BrowserSubprocessPath = browserProcessPath,
            SingleProcess = false,
            MultiThreadedMessageLoop = true,
            LogSeverity = CefLogSeverity.Disable,
            LogFile = "CefGlue.log",
        };

Because I had changed the application output directory,so the "browserProcessPath" is invalid,then I modify the "browserProcessPath" to match the output directory so that the application can find it.

But,I still can not understand the meaning and usage of "browserProcessPath "

Solution 4

You never added the control to the form's controls collection, at least in the samples you showed.

Share:
10,923
Franva
Author by

Franva

I am a software engineer, working on the .NET platform. I am interested in developing mobile apps, Computer Vision, Image Processing, playing guitar and piano, travelling, cooking etc.

Updated on June 04, 2022

Comments

  • Franva
    Franva almost 2 years

    Solution:

    This issue is caused by not loading the CefRuntime in the Program.cs. Simply copying all code in the sample file to your Program.cs.

    And then if your CefWebBrowser is stuck and having "a spinning wait mouse cursor" then set the SingleProcess = true.

    The reason is if the browser is called from a recognized secondary process, the whole process will be blocked until the process is ended.


    Question:

    Anyone has experience using CEF (Chromium Embedded Framework) in WinForm?

    This component has tortured me a whole day. I cannot make it work. It just simply doesn't show up.

    So my question is: how to use CEF in a WinForm application?

    I did check the forum, but surprisingly no one asked this question. It seems like I am the only dumb person who doesn't know how to use it.

    public partial class Form1 : Form
    {
        private CefWebBrowser cefwbShell = null;
        //private readonly SynchronizationContext _pUIThread;
    
        private void Form1_Load(object sender, EventArgs e)
        {
            cefwbShell.Visible = true;
            cefwbShell = new CefWebBrowser { StartUrl = "http://example.com" };
            cefwbShell.Parent = this;
            cefwbShell.Dock = DockStyle.Fill;
            cefwbShell.BringToFront();
            cefwbShell.Show();
        }
    }
    

    Update:

    I am trying to use Xilium.CefGlue. After I copied right version of the release dlls (991) I get an exception:

    InvalidOperationException was unhandlled by user code 
       Failed to create browser.
    

    Source: Xilium.CefGlue

    Stack Trace:

    at Xilium.CefGlue.CefBrowserHost.CreateBrowser(CefWindowInfo windowInfo, CefClient client, CefBrowserSettings settings, String url) in C:\Winston\Knowledge\Projects\xilium-xilium.cefglue-61551ec98ad8\xilium-xilium.cefglue-61551ec98ad8\CefGlue\Classes.Proxies\CefBrowserHost.cs:line 37
       at Xilium.CefGlue.WindowsForms.CefWebBrowser.OnHandleCreated(EventArgs e) in C:\Winston\Knowledge\Projects\xilium-xilium.cefglue-61551ec98ad8\xilium-xilium.cefglue-61551ec98ad8\CefGlue.WindowsForms\CefWebBrowser.cs:line 71
       at System.Windows.Forms.Control.WmCreate(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
       at System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
       at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
       at System.Windows.Forms.Control.CreateHandle()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
    

    Can anyone help me please?

    Update 2:

    private void Create()
            {
                var page = new TabPage("New Tab");
                page.Padding = new Padding(0, 0, 0, 0);
    
                var browser = new CefWebBrowser();
                browser.StartUrl = startUrl;
                browser.Dock = DockStyle.Fill;
                browser.TitleChanged += (s, e) =>
                {
                    BeginInvoke(new Action(() =>
                    {
                        var title = browser.Title;
                        if (tabControl.SelectedTab == page)
                        {
                            Text = browser.Title + " - " + _mainTitle;
                        }
                        page.ToolTipText = title;
                        if (title.Length > 18)
                        {
                            title = title.Substring(0, 18) + "...";
                        }
                        page.Text = title;
                    }));
                };
                browser.AddressChanged += (s, e) =>
                {
                    BeginInvoke(new Action(() =>
                    {
                        addressTextBox.Text = browser.Address;
                    }));
                };
                browser.StatusMessage += (s, e) =>
                {
                    BeginInvoke(new Action(() =>
                    {
                        statusLabel.Text = e.Value;
                    }));
                };
    
                page.Controls.Add(browser);
    
                tabControl.TabPages.Add(page);
    
                tabControl.SelectedTab = page;
            }
    
  • Franva
    Franva about 11 years
    I do add the CefBrowser into a tabPage component which is then added into the tabControl which is already added onto the WinForm's controls collection. See my updated code
  • Franva
    Franva about 11 years
    Hi Sundae, thank you very much! I have solved this problem. Yes it is caused by not loading the CefRuntime in Program.cs. And I have also solved your problem which is the "spinning wait mouse cursor". It is because that the SingleProcess is set to false, so it will block the whole process until it exits if the browser is called from a recognized secondary process. So please see the solution in my post. But I will mark your post as the answer anyway for courtesy because you are the only one who provide relative solution^^ cheers
  • Sundae
    Sundae about 11 years
    Thank you Franva! I will try your solution. Regards
  • Przemysław Michalski
    Przemysław Michalski almost 11 years
    Detailed info about initializing CEF in your app you can find here: code.google.com/p/chromiumembedded/wiki/Architecture
  • PetrV
    PetrV over 8 years
    Though on a Mac throuh Xilium.CefGlue it is only running mode at the moment (SingleProcess=true) , just noting that there are platform differences.