0

I'm using Microsoft Visual Studio 2013 Ultimate and am currently using a Coded UI Test Project. When using it for a Console application, I get the error "An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll" when executing the first line of code below, but I don't get it when using my coded UI test project.

The end intention of my code is to launch a web browser. Click some links, and read information from certain ID fields from within the browser. If someone knows of a better or simply working implementation of doing that, I am all for it.

In my current implementation, internet explorer is never launched.

WebBrowser web = new WebBrowser(); //create object web browser
web.CreateControl();
web.Visible = true;
web.ScriptErrorsSuppressed = true;
web.Navigate("www.gogle.com"); //Shouldn't this launch IE & go to google.com?
HtmlDocument doc = web.Document;
HtmlElement el = doc.GetElementById("hplogo");//get htmlelement on the google logo
//do something with info from el.
web.Dispose(); //de-allocate
user2589339
  • 91
  • 10

1 Answers1

0

From MS KB841295 you can add the STAThread() attribute to your main entry point of your program and this should resolve that specific exception.

[STAThread()]
static void Main(string[] args) {
    WebBrowser web = new WebBrowser(); //create object web browser
    //...
}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274