Threading and webbrowser control

无人久伴 提交于 2019-12-07 23:33:15

问题


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?


回答1:


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";
                }));



回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!