469,626 Members | 872 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Wierd threading issues with WebBrowser control

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
Aug 17 '05 #1
2 14213
Warren <Wa****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Aug 17 '05 #2
"Jon Skeet [C# MVP]" wrote:
Warren <Wa****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by boxim | last post: by
12 posts views Thread by Alex Clark | last post: by
1 post views Thread by JP2006 | last post: by
1 post views Thread by Narasimham | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.