I'm very new to threading. I start a thread like this:
Thread t_main;
t_main = new Thread(main_building_stuff);
t_main.Start();
And at some point, I want in the main_building_stuff to grap some data from an webpage that is loaded into an webbrowser controll in the main thread.
I'm doing that with this piece of code:
HtmlElement lit = webBrowser1.Document.GetElementById("buildqueue");
But that results in an error... InvalidCastException (specified cast is not valid)
What is the proper way to receive the data from the webbrowser?
Somebody that answerd the question, gave the right answer. But for some reason, he deleted it, so thank you, but i don't remember your name...
This was the piece of code that worked:
webBrowser1.Invoke(new Action(() => {
HtmlElement lit = webBrowser1.Document.GetElementById("buildqueue");
result = "whatever";
}));
Hmmm.
System.Windows.Forms.HtmlDocument.GetElementById() definitely returns a System.Windows.Forms.HtmlElement.
What happens if you change
HtmlElement lit = webBrowser1.Document.GetElementById("buildqueue");
to
System.Windows.Forms.HtmlElement lit = webBrowser1.Document.GetElementById("buildqueue");
Do you still get an invalid cast error?
I just happen to know that there are multiple commonly used classes called HtmlElement and I wonder if you've got one from another namespace imported.
来源:https://stackoverflow.com/questions/12386071/threading-and-webbrowser-control