InvalidOperationException appeared after introduction of WebBrowser control


J

Jeroen

We are experiencing a tuff-to-debug problem ever since we introduced a
WebBrowser control into our failry large application. I'm not sure if
the problem description below contains enough details, so if I need to
elaborate on something please ask for it.

We have a UserControl with a WebBrowser on it. This UserControl is
instantiated a few times during the program run, depending on user
interaction. The WebBrowser then navigates to a local HTML file. I
catch the navigating event and always cancel it, instead taking some
custom action.

At irregular and hard-to-reproduce times, we get an
InvalidOperationException when closing the application. The debugger
is not able to open a code file where it happens, it just shows a
popup with the exception. Here's a trace:

------------------------- TRACE -------------------------------

System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate
method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.BeginInvoke(Delegate method,
Object[] args)
at
System.Windows.Forms.WindowsFormsSynchronizationContext.Post(SendOrPostCallback
d, Object state)
at System.Windows.Forms.AxHost.ConnectionPointCookie.Finalize()</
StackTrace><ExceptionString>System.InvalidOperationException: Invoke
or BeginInvoke cannot be called on a control until the window handle
has been created.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller,
Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.BeginInvoke(Delegate method,
Object[] args)
at
System.Windows.Forms.WindowsFormsSynchronizationContext.Post(SendOrPostCallback
d, Object state)
at System.Windows.Forms.AxHost.ConnectionPointCookie.Finalize()</
ExceptionString></Exception></TraceRecord>
The program '[2648] MyApplication.vshost.exe: Managed' has exited with
code 0 (0x0).
------------------------------------------------------------------------

It almost seems like the 'Finalize' method of the webbrowser's
ConnectionPointCookie is being called twice (?).
Is there any way to check this?
Should I be looking for a solution/the problem in different parts of
the code?
 
Ad

Advertisements

N

Nicholas Paldino [.NET/C# MVP]

Jeroen,

I'm thinking it is something else you are doing somewhere else in the
code.

Are you doing anything on another thread, and possibly calling the
webbrowser control then?

Also, the custom actions you are taking when you call Navigate, can you
perform those actions using an asynchronous pluggable protocol?
 
Ad

Advertisements

J

Jeroen

Nope, the webbrowser is just instantiated, and not referenced in other
code. I will be looking into asynchronous pluggable protocols right
after this. Thanks for the tip.

I'm also posting because I thought I had a solution (so maybe this
post will help folks with a simular problem a little bit). In the user
control containing the webbrowser, I now dispose the webbrowser
control manually. This seemed to solve the problem: we get almost no
exceptions in debug mode anymore. However, during tests with a release
build of the software we are re-experiencing this
InvalidOperationException (much less frequent but still (seemingly)
irregularly, also at closing the application).

The dispose method of the usercontrol now looks like this:

/***********************************/
protected override void Dispose( bool disposing )
{
// The following statement improves things
// a lot, but in release mode there still are
// some InvalidOperationExceptions
this.myWebBrowser.Dispose();

if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/**********************************/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top