BackgroundWorker线程在访问Windows窗体控件时失败

时间:2012-11-30 22:00:07

标签: c# winforms backgroundworker

我正在尝试从后台工作进程更新Windows窗体控件,如下面的代码所示。不幸的是,我收到以下错误:

  

跨线程操作无效:控制'richTextBox1'从其创建的线程以外的线程访问。

以下是我正在使用的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        private CookieContainer cookies;
        public Form1() {
            InitializeComponent();
            backgroundWorker1.DoWork += 
                new DoWorkEventHandler(backgroundWorker1_DoWork);    
        }

        public string od_auth(string s) {    
            if (s.Contains(":")) {
                string[] acc = s.Split(':');
                cookies = new CookieContainer();
                HttpWebResponse res;
                try {
                    HttpWebRequest req = 
                        (HttpWebRequest)HttpWebRequest.Create(
                            "http://m.site.ru/dk?" + 
                            "bk=GuestMain" + 
                            "&st.cmd=main" + 
                            "&tkn=1110" +
                            "&fr.posted=set" +
                            "&fr.needCaptcha=" + 
                            "&fr.login=" + acc[0] + 
                            "&fr.password=" + acc[1] +
                            "&fr.remember=on" + 
                            "&button_login=%D0%92%D0%BE%D0%B9%D1%82%D0%B8"
                        );
                    req.AllowAutoRedirect = false;
                    req.Method = "HEAD";
                    req.CookieContainer = cookies;
                    req.UserAgent = "Opera/9.80 (Windows NT 6.1; U; ru) " +
                                    "Presto/2.2.15 Version/10.10";
                    req.ContentType = "application/x-www-form-urlencoded";
                    res = (HttpWebResponse)req.GetResponse();
                    res.Close();

                    string cook = "";
                    try {
                        if (res.Headers["Location"].Contains("cmd=userMain")) {    
                            cook = "AUTHCODE=" + Regex.Match(res.Headers["Set-Cookie"], @"HCODE=(?<id>[^;]+);").Groups["id"].Value + "; ";
                            cook += "JSESSIONID=" + Regex.Match(res.Headers["Set-Cookie"], @"ESSIONID=(?<id>[^;]+);", RegexOptions.RightToLeft).Groups["id"].Value + ";";
                            richTextBox1.Text += "[+] Авторизовались успешно на " + acc[0] + ":" + acc[1] + "\r\n";
                            return cook;
                        } else {
                            cook = "badacc";
                            richTextBox1.Text += "[-] Аккаунт " + acc[0] + ":" + acc[1] + " невалидный\r\n";
                            return cook;
                        }
                    } catch {
                        cook = "badacc";
                        richTextBox1.Text += "[-] Аккаунт " + acc[0] + ":" + acc[1] + " невалидный\r\n";
                        return cook;
                    }
                }
                catch { richTextBox1.Text += "[?] network error \r\n"; ;return "err"; }
            }

            richTextBox1.Text += "[?] some error \r\n";
            return "err";
        }

        public void od_info_changer(string cook) {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://m.site.ru/dk?bk=UserSettingsProfile&st.cmd=userSettingsProfile&tkn=2154");
            request.Method = "POST";
            request.Headers.Add("Cookie: "+cook);
            request.AllowAutoRedirect = false;
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] EncodedPostParams = Encoding.UTF8.GetBytes("fr.posted=set&fr.name=D1%8F&fr.surname=%D0&fr.gender=1&fr.birthday=12&fr.bmonth=10&fr.byear=1949&fr.country=10414533690&fr.city=%D0%95%D0%BA%D0%B0%D1%82%D0%B5%D1%80%D0%B8%D0%BD%D0%B1%D1%83%D1%80%D0%B3&button_save=%D0%A1%D0%BE%D1%85%D1%80%D0%B0%D0%BD%D0%B8%D1%82%D1%8C");
            request.ContentLength = EncodedPostParams.Length;
            request.GetRequestStream().Write(EncodedPostParams, 0, EncodedPostParams.Length);
            request.GetRequestStream().Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string html = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
            if (response.Headers["Location"].IndexOf("st.cmd=userSettings") != -1) {
                richTextBox1.Text += "[+] Cменили пароль\r\n";
            } else {
                richTextBox1.Text += "[-] Не смогли сменить имя\r\n";
            }               
        }

        List<string> accs=new List<string>();
        private void открытьАккаунтыToolStripMenuItem_Click(object sender, EventArgs e) {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK) {
                accs.Clear();
                foreach (string s in File.ReadAllLines(openFileDialog1.FileName))
                    accs.Add(s);

                richTextBox1.Text += "[+] Загрузили " + accs.Count.ToString() + " аккаунтов\n";
            }
        }

        private void Auth_Click(object sender, EventArgs e) {
            Auth.Enabled = false; // На время расчета блокируем опасные кнопки
            backgroundWorker1.RunWorkerAsync();
        }    

        public void go() {
            foreach (string acc in accs) {
                string cook = od_auth(acc);
                if (cook!="badacc")
                    od_info_changer(cook);
            }    
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
            go();
        }
    }
}

但是,在加载.txt文件并按下我的按钮后,我会收到:

this error message

出了什么问题?

4 个答案:

答案 0 :(得分:3)

您无法直接从后台线程更新控件。您需要调用回主线程并在那里执行操作:

delegate void updateDelegate(string val);

private void UpdateText(string updateVal)
{
    if (richTextBox1.InvokeRequired)
        richTextBox1.Invoke(new updateDelegate(UpdateText), updateVal);
    else
        richTextBox1.Text += updateVal;

}

然后在后台工作人员代码中,您可以调用richTextBox1.text方法,而不是设置UpdateText

UpdateText("Whatever Value you want");

答案 1 :(得分:2)

您正在尝试与后台任务中的UI控件进行交互。你不能这样做。您需要放置需要一段时间才能运行的代码(在本例中为HTTP请求),并且不与DoWork处理程序中的UI交互,并且在RunWorkerCompleted中显示结果的代码事件处理程序。已完成的事件将始终在UI线程中运行,进度更改事件也将如此。

当您需要定期从后台任务更新UI时,通常会指示进度已更改。在这种情况下,您可以订阅相关的进度更改事件,然后通过ReportProgress定期将当前进度传递给后台工作人员。

您可以在UI控件上使用Invoke来编组后台线程中的UI线程,但是您应该避免在大多数情况下这样做。使用BackgroundWorker的全部原因是为了照顾您。您的工作只是将适当的代码放在适当的事件处理程序中。如果你需要使用Invoke来处理一些奇怪的边缘情况,这有时会更容易,但是如果你经常使用它,那就表明你没有正确使用现有的更高级别的抽象。

答案 2 :(得分:0)

如果您希望从后台工作者处理您的表单,则应使用Control.InvokeHere是解释和一些例子。

答案 3 :(得分:0)

你必须安全地调用:

if (richtextBox1.InvokeRequired)
{
 richtextBox1.Invoke(//Delegate);
}