Unhandled exception on exit

61 views
Skip to first unread message

Jeff Dege

unread,
May 25, 2016, 4:02:34 PM5/25/16
to CefSharp
I'm trying to evaluate whether CefSharp would work for us, in a XAML/WPF application.

Currently, we're working with MS's WebBrowser, which has severe limitations.

What we have, in our app, is a window which displays one of several custom controls, one of which contains a browser control that loads a web page containing a map.

For testing, I've created a custom control that contains a WebBrowser hard-coded to http://www.google.com.

I then created a second custom control that contains a ChromiumWebBrowser hard-coded to http://www.google.com.

I'm doing nothing with either browser control in code, I'm simply making the user controls that wrap them visible or collapsed.

But if I have the ChromiumWebBrowser included in the XAML, whether I make it visible or not, on exit I get an exception:

System.InvalidOperationException was unhandled
Message: An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll
Additional information: The calling thread cannot access this object because a different thread owns it.

Just to make it clear, if my custom control contains this, I do not get an exception:

<KtWpf:KorUserControl 
        x:Class="KtWpf.CEFSharpUtilityMap"
        xmlns:KtWpf="clr-namespace:KtWpf" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
        >
    <DockPanel>
        <TextBox DockPanel.Dock="Top">CEFSharp</TextBox>
        <WebBrowser
                x:Name="mapBrowser"
                Source="http://www.google.com"
                />
    </DockPanel>
</KtWpf:KorUserControl>


If it contains this, I do:
<KtWpf:KorUserControl 
        x:Class="KtWpf.CEFSharpUtilityMap"
        xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
        xmlns:KtWpf="clr-namespace:KtWpf" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
        >
    <DockPanel>
        <TextBox DockPanel.Dock="Top">CEFSharp</TextBox>
        <cefSharp:ChromiumWebBrowser 
                x:Name="mapBrowser" 
                Address="http://www.google.com"
            />
    </DockPanel>
</KtWpf:KorUserControl>


Any ideas?

I'm using CefSharp.Wfp version 49.0.0, from NuGet. (And CefSharp.Common and cef.redist.x64, though I get the same thing building for x86.)


Ritchie Annand

unread,
Jun 6, 2016, 11:50:49 AM6/6/16
to CefSharp
I've noticed ChromiumWebBrowser inside WPF makes for a lot pickier experience about which thread created what.

For example, I hooked up an event to the browser's FrameLoadEnd. With the MS control, I could do this:

    if (WindowState == WindowState.Minimized || Visibility != Visibility.Visible)

With the CefSharp control, this complains in exactly the manner you describe. You can't even ask what the WindowState is, never mind set it.

I put the event handler in a separate method and called it this way:

Dispatcher.Invoke(() => FrameLoadEndHandler(sender, nowVisible));

or

Application.Current.Dispatcher.Invoke(() => FrameLoadEndHandler(sender, nowVisible));

I don't know if that at all helps with the issue you have there, but just to let you know that events in the CefSharp stuff are not necessarily the same as WPF's main UI thread and that can trigger such warnings.

-- Ritchie Annand

Jeff Dege

unread,
Jun 6, 2016, 11:56:00 AM6/6/16
to CefSharp
On Monday, June 6, 2016 at 10:50:49 AM UTC-5, Ritchie Annand wrote:
I've noticed ChromiumWebBrowser inside WPF makes for a lot pickier experience about which thread created what.

For example, I hooked up an event to the browser's FrameLoadEnd. With the MS control, I could do this:

    if (WindowState == WindowState.Minimized || Visibility != Visibility.Visible)

With the CefSharp control, this complains in exactly the manner you describe. You can't even ask what the WindowState is, never mind set it.

I put the event handler in a separate method and called it this way:

Dispatcher.Invoke(() => FrameLoadEndHandler(sender, nowVisible));

or

Application.Current.Dispatcher.Invoke(() => FrameLoadEndHandler(sender, nowVisible));

I don't know if that at all helps with the issue you have there, but just to let you know that events in the CefSharp stuff are not necessarily the same as WPF's main UI thread and that can trigger such warnings.

-- Ritchie Annand


What we need to do is to take control of the initialization and shutdown.

In App.OnStartup():

protected override void OnStartup(StartupEventArgs e)
{
   
base.OnStartup(e);
   
Cef.Initialize();
   
...
}


and in App.OnExit():

protected override void OnExit(ExitEventArgs e)
{
   
Cef.Shutdown();
   
base.OnExit(e);
}


This solved my immediate problem. There are probably others lurking, we've not yet encountered.
 
Reply all
Reply to author
Forward
0 new messages