Closing ChromiumWebBrowser instance causes app to freeze

2,119 views
Skip to first unread message

Tyler Jones

unread,
Oct 7, 2015, 6:26:02 PM10/7/15
to CefSharp
I have a winforms form that instantiates a ChromiumWebBrowser instance in it's constructor, when the form is initialized.  I have a FormClosing() function that runs whenever the form closes both from clicking the "x" button, and when calling .Close().  When I close the form using the "x" button, i'm able to dispose the Browser object without an issue, in the FormClosing method.  However, if I call .Close() on the form, when I .Dispose() line is hit in FormClosing() on that same Browser object, my app freezes for about 30 seconds, leading me to believe the Browser object is on a different thread.  

However, if I check .InvokeRequired on the Browser object, it returns false.  How can I dispose of the ChromiumWebBrowser instance without causing the app to freeze?  Why does .Dispose() work fine when clicking the "x" button?

Here is my constructor:

public PhxCEFBrowser(string url)
{
InitializeComponent();

Browser = new ChromiumWebBrowser(url)
{
Dock = DockStyle.Fill,
Location = new Point(0, 0),
MinimumSize = new Size(20, 20),
Name = "webBrowser1",
Size = new Size(284, 261),
TabIndex = 0,
};
Browser.BrowserSettings.ApplicationCacheDisabled = true;
Browser.KeyboardHandler = new KeyboardHandler();

browserPanel.Controls.Add(Browser);

        ClientSize = new Size(284, 261);
        Name = "WebBrowser";
        ResumeLayout(false);
}

And here is my FormClosing() handler:

private void PhxCEFBrowser_FormClosing(object sender, FormClosingEventArgs e)
{
browserPanel.Controls.Remove(Browser);
if (Browser.InvokeRequired)//<-- Returns false
{
Browser.Invoke(new MethodInvoker(delegate
{
Browser.Dispose();//<-- never gets hit
}));
}
else
{
Browser.Dispose();//<-- gets hit when calling .Close(), causing the app to freeze.  works fine without freezing when clicking the "x" button
}
}

Alex Maitland

unread,
Oct 8, 2015, 4:34:39 AM10/8/15
to CefSharp
Version?

Alex Maitland

unread,
Oct 8, 2015, 5:14:24 AM10/8/15
to CefSharp
Also does your app exit if you remove all calls to `Browser.Dispose`?

`WinForms` will actually call `Dispose` for you, so it's not absolutely necessary to do that explicitly.


On Thursday, 8 October 2015 08:26:02 UTC+10, Tyler Jones wrote:

Tyler Jones

unread,
Oct 8, 2015, 11:49:00 AM10/8/15
to CefSharp
If I remove the manual .Dispose() calls, the app still freezes, but *after* the window is closed.  The NuGet package I'm using says the version is 41.0.1

Tyler Jones

unread,
Oct 8, 2015, 12:57:54 PM10/8/15
to CefSharp
I just noticed there's a version 43, so I upgraded to 43, and it's still behaving exactly the same way.  If I call .Close() on the form, it freezes.  If I close the form with the "x" button, it shuts down instantly with no freezing.  This happens whether or not I manually call .Dispose().

Tyler Jones

unread,
Oct 8, 2015, 3:42:32 PM10/8/15
to CefSharp
Wow, i finally figured this out after many hours.  I was registering a class as my JsObjectForScripting, then calling .Close() from that same class.  If I start up a new Task, then close the window from the new task, it closes with no freezing.  Aparently the problem was because I was closing the form from the same class as the one that was registered as the JsObjectForScripting.

Jim Shattuck

unread,
Jul 20, 2018, 12:57:08 PM7/20/18
to CefSharp
Thanks this helped me with similar DotNetBrowser chromium issue.  I ended up using    Task t = Task.Run(() => {Dispatcher.Invoke(//Method that calls _webView.Dispose()}));
Reply all
Reply to author
Forward
0 new messages