Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
First, I'll give you the background information. I have an MDI application that I've written in C#, .NET 2.0. The app's Main() is decorated with [STAThread]. There are three child windows that use a "report window" to display data in a WebBrowser control; let's call them Window 1, 2 and 3. I pass XML as a string from each of the three windows as a parameter to the constructor. That string is then transformed to HTML using an XslCompiledTransform object. When the report window is opened, it is being opened as a Dialog window.

If I open window 1 and request the report, everything works as expected. If I dismiss the dialog, open window 2 and request the report, again, everything works as expected. Window 3, same thing. Here is where the weirdness starts. If I open window 1, run the report, open either other window, run the report, then open window 1 again and run the report, I get an exception (ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment) in the Visual Studio-generated InitializeComponent() code implying that I have a threading model problem when it does the following: this.webBrowser = new System.Windows.Forms.WebBrowser();

How can this be? The main() function is clearly marked as [STAThread]. I even added "Thread.CurrentThread.SetApartmentState(ApartmentState.STA);" to the constructor before the InitializeComponent() call; no luck. The only controls on that window are the WebBrowser control and two standard buttons. To make things weirder, if I do an open/request on window 1, I can do an unlimited number of open/request actions on the other two windows. It's only when I go back to window 1 that the exception is thrown.

Can anyone shed any light at all on this subject? I've exhausted all of the Google queries I can think of.

Thanks in advance.

Tim
Posted

1 solution

Just out of curiosity, have you tried moving to .Net 4 and see if the behavior persists (just as a test)?
 
Share this answer
 
Comments
TimWallace 1-Sep-11 15:16pm    
John:

Thanks for your suggestion. I don't have 4.0 installed, but I did try 3.5 and I receive the same results.

Tim

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900