Wierd threading issues with WebBrowser control

G

Guest

I don't understand what's going on in the following program. I believe it
should simply create a form with a browser in it and navigate that browser to
Google. However, the constructor for the WebBrowser is throwing a
ThreadStateException saying ActiveX control
'8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the
current thread is not in a single-threaded apartment. Any ideas what's going
on?

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Project1
{
class Class1
{
public static void Main()
{
Application.Run(new Form1());
}
}

class Form1 : Form
{
public Form1()
{
WebBrowser b = new WebBrowser();
b.Navigate("www.google.com");
this.Controls.Add(b);
}
}
}

Thanks
-Warren
 
J

Jon Skeet [C# MVP]

Warren said:
I don't understand what's going on in the following program. I believe it
should simply create a form with a browser in it and navigate that browser to
Google. However, the constructor for the WebBrowser is throwing a
ThreadStateException saying ActiveX control
'8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the
current thread is not in a single-threaded apartment. Any ideas what's going
on?

It means exactly what it says - you're not running in an STA. If you
put the [STAThread] attribute on your Main method, you should be fine.
 
G

Guest

Jon Skeet said:
Warren said:
I don't understand what's going on in the following program. I believe it
should simply create a form with a browser in it and navigate that browser to
Google. However, the constructor for the WebBrowser is throwing a
ThreadStateException saying ActiveX control
'8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the
current thread is not in a single-threaded apartment. Any ideas what's going
on?

It means exactly what it says - you're not running in an STA. If you
put the [STAThread] attribute on your Main method, you should be fine.

Thanks a bunch! I'd seen that on Windows Forms Designer generated code, but
never had any idea what it did.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top