Click here to Skip to main content
11,413,961 members (74,276 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Threading Exception
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-Sep-11 9:58am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Just out of curiosity, have you tried moving to .Net 4 and see if the behavior persists (just as a test)?
  Permalink  
Comments
TimWallace at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,035
1 OriginalGriff 7,387
2 Maciej Los 3,640
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web04 | 2.8.150427.2 | Last Updated 1 Sep 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100