views:

557

answers:

1

VS2008, .NET 2, VB.NET, XP ...

I have a Windows form, with a WebBrowser control and a Close button, which just does a Me.Close. The form's cancel button is set to the Close button, so that I can hit ESC to close the form.

I set the DocumentText property of the WebBrowser control in the load event, and the HTML displays.

Running the application from Visual Studio, if I click the Close button, the form closes with no error.

If I hit the ESC button I get

RaceOnRCWCleanup was detected Message: An attempt has been made to free an RCW that is in use. The RCW is in use on the active thread or another thread. Attempting to free an in-use RCW can cause corruption or data loss.

If I run the app outside VS, I get no error.

Any ideas a) why the error, and b) how to prevent or suppress it?

Many thanks in advance.

+2  A: 

It is not an error, it is a warning. Produced by a Managed Debugging Assistant (MDA), an extension to the debugger for managed code, that thinks it is seeing something going wrong in your code. The shoe fits. You are using an RCW, WebBrowser is an COM control. You are killing off the RCW, you are closing your form. The MDA steps in because it thinks it is seeing the web browser in use and it getting killed before the request is completed. That would normally only make sense if you are using a thread in your code.

Are you? If not, don't lose any sleep over it. COM uses reference counting, notorious for not being able to resolve circular references.

Hans Passant
No, it's all happening on the foreground thread. It's not the fact that it's happening at all that bothers me - there are ways of switching them off in VS. It's the fact that if the Close event handler is invoked by clicking the Close button, it *doesn't* happen, but if the same handler is invoked by hitting ESC (since the Close button is the form's Cancel button property), it *does*.Why the difference?
ChrisA
I'm seeing this exact scenario.. it isn't a problem for me, because I WANT to close the form with the webBrowser on it. I would like to understand why there is a difference between pressing ESC and clicking the 'X'.
mpeterson