线程中的 C# webbrowser getElement [英] C# webbrowser getElement in Thread

查看:27
本文介绍了线程中的 C# webbrowser getElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

我创建它是为了测试在 Internet 站点上单击按钮的功能.现在我想一遍又一遍地自动化几个步骤,包括在一个线程中的这个片段.

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.

代码:

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

现在的问题是,如您所见,我在线程中执行了一次基本相同的代码片段,而在没有线程的情况下执行了一次.当我在没有线程的情况下执行它时,它工作正常.但是在线程中,我在方法 checkForNew

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

推荐答案

您不需要额外的线程来自动化 WebBrowser 控件.使用来自创建 WebBrowser 的同一线程的异步逻辑.我用一些值得一试的示例代码回答了类似的问题:

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:

界面应用:

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

控制台应用:

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

这篇关于线程中的 C# webbrowser getElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆