73

I need to get information about applied CSS styles in HTML page. I used AxWebBrowser and iterate IHTMLDOMNode. I'm able to get all the data I need and move the code into my application. The problem is that this part is running inside of the background worker and I got exception when trying to instantiate the control.

AxWebBrowser browser = new AxWebBrowser();

ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated
because the current thread is not in a single-threaded apartment.

Is there any way how to solve this or other option than AxWebBrowser?

martin.malek
  • 2,166
  • 2
  • 19
  • 31

4 Answers4

95

The problem you're running into is that most background thread / worker APIs will create the thread in a Multithreaded Apartment state. The error message indicates that the control requires the thread be a Single Threaded Apartment.

You can work around this by creating a thread yourself and specifying the STA apartment state on the thread.

var t = new Thread(MyThreadStartMethod);
t.SetApartmentState(ApartmentState.STA);
t.Start();
Factor Mystic
  • 26,279
  • 16
  • 79
  • 95
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    Thanks, great this is working. One more questions/problem. The class I'm using is just class and the AxWebBrowser looks like it needs to be added into this.Controls(). Is there way how to fake the Controls? Or will I need to have separated Form for that? – martin.malek Sep 13 '09 at 18:36
  • @martin.malek There's no great way to fake that. The best bet is to create a new form. – JaredPar Sep 13 '09 at 18:46
  • 1
    Hi, the code should be t.SetApartmentState(ApartmentState.STA); – Santiago Corredoira Nov 28 '10 at 12:55
  • Confirmed Dawkins comment, I also had to change it to AppartmentState.STA. – Boog Mar 08 '11 at 18:37
  • I've met this error when I want to open a windowsfrom from my XNA-Game. I've opened the form with JaredPar's code and it works. Can I carry this code inside the windows form's code ? – icaptan Sep 07 '11 at 22:19
  • 2
    Beware that this code is not enough to properly implement an STA thread, it must also pump a message loop. Particularly WebBrowser will malfunction, it will not fire its DocumentCompleted event. Check [this post](http://stackoverflow.com/a/21684059/17034) for an alternative. – Hans Passant Apr 18 '14 at 10:29
  • How do you do this in the Compact Framework. (i.e. compact framework has no `[STAThread]` ability.) – bvdb Jul 28 '16 at 10:02
  • I am using Task, is there any solution? – camino Dec 25 '21 at 11:50
66

Go ahead and add [STAThread] to the main entry of your application, this indicates the COM threading model is single-threaded apartment (STA)

example:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new WebBrowser());
    }
}
user764177
  • 661
  • 5
  • 2
4

If you used [STAThread] to the main entry of your application and still get the error you may need to make a Thread-Safe call to the control... something like below. In my case with the same problem the following solution worked!

Private void YourFunc(..)
{
    if (this.InvokeRequired)
    {
        Invoke(new MethodInvoker(delegate()
        {
           // Call your method YourFunc(..);
        }));
    }
    else
    {
        ///
    }
Ahmad
  • 8,811
  • 11
  • 76
  • 141
  • What should be invoked in here? The browser? In which state? In my case the browser is sitting on a dialog. – C4d Nov 08 '17 at 07:47
0

My problem with this was that I had my Main method marked as async. So fixed it just by making it static void and adding .wait() to all awaitable methods inside.

Grengas
  • 836
  • 12
  • 16