2

我正在尝试从后台工作进程更新 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文件并按下我的按钮后,我收到:

此错误消息

怎么了?

4

4 回答 4

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");
于 2012-11-30T22:07:40.777 回答
2

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

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

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

于 2012-11-30T22:13:39.087 回答
0

Control.Invoke如果您想使用后台工作人员的表单,您应该使用。这里有解释和一些例子。

于 2012-11-30T22:04:53.787 回答
0

您必须安全调用:

if (richtextBox1.InvokeRequired)
{
 richtextBox1.Invoke(//Delegate);
}
于 2012-11-30T22:08:34.913 回答