C# webbrowser getElement in Thread

一世执手 提交于 2019-12-11 06:25:49

问题


I have the following code:

private void button5_Click(object sender, EventArgs e)
    {
        HtmlElementCollection elc = webBrowser1.Document.GetElementsByTagName("div");
        string text = "Out of sync ... stopping now!";

        foreach (HtmlElement el in elc)
        {
            if (el.GetAttribute("id").Equals("double"))
            {
                if (el.InnerText != "")
                {
                    text = el.InnerText;
                }
            }
        }
        MessageBox.Show(text);
    }

I created this to test the functionality to click buttons on internet sites. Now i want to automate a few steps over and over, including this snippet in a Thread.

Code:

private void button4_Click(object sender, EventArgs e)
    {

        button4.Enabled = false;
        Thread t = new Thread(new ThreadStart(ThreadJob));
        t.Start();
    }

    private void ThreadJob()
    {
        string text = "";
        int countLoss = 0;

        while (running)
        {
            text = checkForNew();
            Thread.Sleep(100);
            if (text.Equals(""))
            {
                for (int i = 0; i < countLoss; i++)
                {
                    halfit();
                }
                countLoss = 0;
                bet();
                Thread.Sleep(2000);
            }
            else
            {
                countLoss++;
                Thread.Sleep(2000);
            }

        }
    }

public string checkForNew()
    {
        HtmlElementCollection elc = webBrowser1.Document.GetElementsByTagName("div");
        string text = "";

        foreach (HtmlElement el in elc)
        {
            if (el.GetAttribute("id").Equals("double"))
            {
                if (el.InnerText != "")
                {
                    if (el.InnerText != null)
                    {
                        text = el.InnerText;
                    }
                }
                else text = "";
            }
        }
        return text;
    }

The Problem is now, as you see i execute basically the same code snippet once in a Thread and once without a Thread. When I execute it without the Thread, it works fine. But in the Thread I get a InvalidCastException at the HtmlElementCollection elc = webBrowser1.Document.GetElementsByTagName("div"); line in the method checkForNew


回答1:


You don't need an additional thread to automate WebBrowser control. Use asynchronous logic from the same thread the WebBrowser is created on. I answered similar questions with some sample code worth checking out:

UI apps:

https://stackoverflow.com/a/19063643/1768303
https://stackoverflow.com/a/20934538/1768303

Console app:

https://stackoverflow.com/a/19718530/1768303




回答2:


From the WebBrowser class documentation:

The WebBrowser class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

So without seeing the rest of your apps code I suspect you will need to give up on touching your UI with the second thread.



来源:https://stackoverflow.com/questions/21014862/c-sharp-webbrowser-getelement-in-thread

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