使用线程时为什么会出现此错误?

时间:2013-08-20 03:26:09

标签: c# post listbox

所以,我让我的程序工作,但是当我运行它时它变得没有响应,所以我决定在一个线程中运行它。现在,我保持一切相同,但我没有使用按钮直接运行代码,而是使用一个按钮来运行包含代码的线程。该程序正在做的是创建一个网页请求,从网页获取cookie,然后运行一个数字列表,使用这些数字创建不同的POST请求使用cookie登录。

工作:

private void button3_Click(object sender, EventArgs e)
    {
        string cookie = webBrowser1.Document.Cookie;
        List<string> removals = new List<string>();
        foreach (string s in listBox1.Items)
        { 
          //do stuff
        }
    }

不工作:

thread th;
    public void thread()
        {
            string cookie = webBrowser1.Document.Cookie;
            List<string> removals = new List<string>();
            foreach (string s in listBox1.Items)
            {
             //do stuff
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            th = new Thread(thread);
            th.Start();
        }

错误:http://prntscr.com/1mabtb

谢谢。

1 个答案:

答案 0 :(得分:0)

您可能正在使用WinForms,在这种情况下,您应该调用webBrowser1控件上的操作,以便在UI线程而不是新创建的线程上执行您的交互。在这里查看一系列答案:

Winforms - Invoking a method in another thread

相关问题