Wierd threading issues with WebBrowser control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Warren

    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
    ThreadStateExce ption 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.Collecti ons.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.A dd(b);
    }
    }
    }

    Thanks
    -Warren
  • Jon Skeet [C# MVP]

    #2
    Re: Wierd threading issues with WebBrowser control

    Warren <Warren@discuss ions.microsoft. com> wrote:[color=blue]
    > 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
    > ThreadStateExce ption 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?[/color]

    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 - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Warren

      #3
      Re: Wierd threading issues with WebBrowser control

      "Jon Skeet [C# MVP]" wrote:
      [color=blue]
      > Warren <Warren@discuss ions.microsoft. com> wrote:[color=green]
      > > 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
      > > ThreadStateExce ption 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?[/color]
      >
      > 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 - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too
      >[/color]

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

      Comment

      Working...