Click here to Skip to main content
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 Mathew Soji 309
1 BillWoodruff 300
2 Afzaal Ahmad Zeeshan 252
3 DamithSL 225
4 Maciej Los 215
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 1 Sep 2011
Copyright © CodeProject, 1999-2014
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