"thread is not in a single-threaded apartment" error if I so much as sneeze RRS feed

  • Question

  •   I have a C# Windows Forms project in VS2008 that uses the WebBrowser control.  By default everything is fine, but as soon as I do any number of things to the project I get the error message:
    ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
      This error occurs at the line:   
    this.txtOut = new System.Windows.Forms.WebBrowser();
      At the InitializeComponent() method of the main form.

      The changes that trigger this error include:
    • Referencing another project
    • Changing the project's output path from "bin\Debug\" to "..\bin\Debug\"  (though it'll work if I run the executable outside of the IDE.)
    • Other things that I've done but can't remember specifically at this time... sorry
      The first two items are reversible (though referencing another project is pretty crucial) but there are a few things (again, sorry that I can't remember what it was that I did) that caused me to have to create a new project and copy all of the sources/resources back into it to fix the issue.
      I do have [STAThread] above the Main() method.  I am in no way implementing any threading scheme in either this project or the one that I'm interested in referencing... oh yeah, the referencing project uses LuaInterface if that makes any difference.
      I'm not sure what further information will be required for anyone willing to try and help me resolve this.  Just ask and I'll happily provide.

      Thanks for any help that you are willing to give.
    • Edited by guanohead Friday, February 19, 2010 1:59 PM
    Friday, February 19, 2010 1:28 PM

All replies

  •   I just found one of the actions that permanently borked the project and required that I create a new project and copy all of the source back in:
      I copied all of the source files from the project that I want to reference into the project with the webbrowser.  I also needed to add a reference to LuaInterface, which I did.  This created the dreaded, single-threaded apartment error.  In addition, once I took the files out of the project and unreferenced LuaInterface... I still received the error.  It continued even after I physically deleted the files from the system!
      I created a new project and copied the files back in and it worked again.  At this point I tried referencing LuaInterface again.  I received no errors.  Removed the reference again and still no errors.
      Incidentally, I am certain that I removed all of the source files properly, the referenced project is well organized and straightforward.  The only nonstandard library it employs is .net binary serialization.

      This is killing me!

    Friday, February 19, 2010 1:57 PM
  • Add the [StaThread] attribute to your main function.

    Friday, February 19, 2010 4:22 PM
  • It's there.  It was there when I created the project and I've never removed it.
    I've also tried adding it to the InitializeComponent() method (just in case) but that didn't help.
    Friday, February 19, 2010 4:47 PM
  • No ideas?  Pardon the bump but it's galling to require such strict regulations on my project, especially when they seem so arbitrary.
    Wednesday, February 24, 2010 4:31 PM