Reputation: 1149
So what I am wanting to do is be able to load 2 URL's at the same time in the same form with 2 cefsharp browser windows
This example image will help explain a bit better
I can't seem to figure out how to do this. At the moment I can only seem to get one instance. If I try run 2 I get this error:
An unhandled exception of type 'System.Exception' occurred in CefSharp.Core.dll
Additional information: Cef can only be initialized once. Use Cef.IsInitialized to guard against this exception.
If this can't be done can somone recommend another HTML5 supported browser for C#?
public ChromiumWebBrowser Browser;
CefSettings settings = new CefSettings();
void InitBrowser()
{
settings.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2764.0 Safari/537.36";
Cef.Initialize(settings);
Browser = new ChromiumWebBrowser("about:blank");
Browser.FrameLoadEnd += OnFrameLoadEnd;
Browser.LoadingStateChanged += OnLoadingStateChanged;
Browser.FrameLoadStart += OnFrameLoadStart;
Browser.LoadError += OnLoadError;
Controls.Add(Browser);
Browser.Dock = DockStyle.Fill;
}
Upvotes: 3
Views: 4358
Reputation: 19
You can add several groupBox in Form and add "ChromiumWebBrowser" to groupBox. Example:
string url1 = "https://www.url1.com";
string url2 = "https://www.url2.com";
string url3 = "https://www.url3.com";
CefSettings settings = new CefSettings();
// Initialize cef with the provided settings
Cef.Initialize(settings);
// Create a browser component
ChromiumWebBrowser chromeBrowser1;
chromeBrowser1 = new ChromiumWebBrowser(url1);
// Add it to the form and fill it to the form window.
groupBox1.Controls.Add(chromeBrowser1);
chromeBrowser1.Dock = DockStyle.Fill;
ChromiumWebBrowser chromeBrowser2;
chromeBrowser2 = new ChromiumWebBrowser(url2);
groupBox2.Controls.Add(chromeBrowser2);
chromeBrowser2.Dock = DockStyle.Fill;
ChromiumWebBrowser chromeBrowser3;
chromeBrowser3 = new ChromiumWebBrowser(url3);
groupBox3.Controls.Add(chromeBrowser3);
chromeBrowser3.Dock = DockStyle.Fill;
Upvotes: 1
Reputation: 3065
You can only initialize CEF once, per application.
There's even a topic in the github CefSharp repository here.
You can only initialize CEF once, per application. Nothing has changed in regards to this. It's just how CEF functions.
Although the architecture of CEF and CefSharp (which is merely a .NET binding on top of it) is different: https://bitbucket.org/chromiumembedded/cef/wiki/Architecture#markdown-header-process-considerations. There is a one common Browser process which then spawn a Renderer process for each "window" or "tab". (You can see them in your Windows Task Manager)
Basically it's not possible, not even in different forms.
You can check if there's already any instance running using the following snippet (maybe you can stop the first, and then start another, although in your case seems to be useless):
if (Cef.IsInitialized)
{
Console.WriteLine("Sorry, cannot start other instance because there's already an open browser");
}
Upvotes: 0