C# - 程序冻结

时间:2018-03-25 01:56:12

标签: c# multithreading

我做了代理检查,出于某种原因我点击了"检查"按钮,程序只是冻结,直到工作完成。

我尝试将代码移到Backgroundworker:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        WebProxy myproxy;
        foreach (string proxy in Proxies)
        {
            try
            {
                myproxy = new WebProxy(proxy);
                WebRequest request = WebRequest.Create("https://www.google.co.il/");
                request.Timeout = TimeOut;
                request.Proxy = myproxy;
                WebResponse re = request.GetResponse();
                richTextBox1.AppendText(proxy + "\r\n");
                Good++;
                label3.Text = "Good: " + Good.ToString();
            }
            catch (Exception)
            {
                Bad++;
                label4.Text = "Bad: " + Bad.ToString();
            }
        }
        MessageBox.Show("Done");
    }

但出于某种原因我收到了这个错误:

  

System.InvalidOperationException:'跨线程操作无效:   Control' richTextBox1'从线程以外的线程访问它   是在。'

上创建的

所以我试图删除label3.Text和label4.Text。 但后来我发现当我点击" Check"按钮。

所以我尝试添加MessageBox.Show(" some text")以查看它是否正常工作,我看到了消息框,但看起来线程跳过了检查。

我该怎么办?

"检查"按钮代码:

 label1.Text = "Lines: 0";
        richTextBox1.Clear();
        backgroundWorker1.RunWorkerAsync();

完整代码:

 public partial class Form1 : Form
{
    WebClient X = new WebClient();
    string url = "";
    ArrayList Proxies = new ArrayList();
    int TimeOut = 3000, Good = 0, Bad = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (radioHTTP.Checked)
            {
                byte[] proxies = X.DownloadData(new Uri("https://www.free-proxy-list.net/"));
                MatchCollection M = Regex.Matches(Encoding.UTF8.GetString(proxies, 0, proxies.Length), "\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}</td><td>\\d{1,6}");
                foreach (Match m in M)
                    richTextBox1.AppendText(m.Value.Replace("</td><td>", ":") + "\r\n");
            }
            if (radioSocks.Checked)
            {
                byte[] proxies = X.DownloadData(new Uri("https://www.socks-proxy.net/"));
                MatchCollection M = Regex.Matches(Encoding.UTF8.GetString(proxies, 0, proxies.Length), "\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}</td><td>\\d{1,6}");
                foreach (Match m in M)
                    richTextBox1.AppendText(m.Value.Replace("</td><td>", ":") + "\r\n");
            }
            if (radioSSL.Checked)
            {
                byte[] proxies = X.DownloadData(new Uri("https://www.sslproxies.org/"));
                MatchCollection M = Regex.Matches(Encoding.UTF8.GetString(proxies, 0, proxies.Length), "\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}</td><td>\\d{1,6}");
                foreach (Match m in M)
                    richTextBox1.AppendText(m.Value.Replace("</td><td>", ":") + "\r\n");
            }
            if (radioCustom.Checked)
            {
                byte[] proxies = X.DownloadData(new Uri(url));
                MatchCollection A = Regex.Matches(Encoding.UTF8.GetString(proxies, 0, proxies.Length), "\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}</td><td>\\d{1,6}");
                MatchCollection B = Regex.Matches(Encoding.UTF8.GetString(proxies, 0, proxies.Length), "\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}\\d{1,6}");
                MatchCollection C = Regex.Matches(Encoding.UTF8.GetString(proxies, 0, proxies.Length), "\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}\",\"port\":\"\\d{2,6}");
                MatchCollection D = Regex.Matches(Encoding.UTF8.GetString(proxies, 0, proxies.Length), "\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}</td><td class=\"column-2\">\\d{2,6}");
                foreach (Match m in A)
                    richTextBox1.AppendText(m.Value.Replace("</td><td>", ":") + "\r\n");
                foreach (Match m in B)
                    richTextBox1.AppendText(m.Value + "\r\n");
                foreach (Match m in C)
                    richTextBox2.AppendText(m.Value.Replace("\",\"port\":\"", ":") + "\r\n");
                foreach (Match m in D)
                    richTextBox2.AppendText(m.Value.Replace("</td><td class=\"column-2\">", ":") + "\r\n");
            }
            string[] lines = richTextBox1.Lines;
            foreach (string line in lines)
                Proxies.Add(line);
            label1.Text = "Lines: " + Proxies.Count;
        }catch
        {

        }

    }
    private void button4_Click(object sender, EventArgs e)
    {
        label1.Text = "Lines: 0";
        richTextBox1.Clear();
        backgroundWorker1.RunWorkerAsync();
    }
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // backgroundWorker1.ReportProgress(0, "Working...");
        WebProxy myproxy;
        foreach (string proxy in Proxies)
        {
            try
            {
                myproxy = new WebProxy(proxy);
                WebRequest request = WebRequest.Create("https://www.google.co.il/");
                request.Timeout = TimeOut;
                request.Proxy = myproxy;
                WebResponse re = request.GetResponse();
                richTextBox1.AppendText(proxy + "\r\n");
                Good++;
                this.Invoke((Action)delegate {
                    label3.Text = "Good: " + Good.ToString();
                });
            }
            catch (Exception)
            {
                Bad++;
                this.Invoke((Action)delegate {
                    label4.Text = "Bad: " + Good.ToString();
                });
            }
        }
        //backgroundWorker1.ReportProgress(100, "Done");
    }

非常感谢

1 个答案:

答案 0 :(得分:1)

WinForms控件不能在创建它们的线程之外修改。

所以做这样的事情:

label3.Text = "Good: " + Good.ToString();

后台工作程序将失败,因为后台工作程序任务在单独的线程中运行。

要解决此问题,您可以在表单的线程上调用委托,如下所示:

this.Invoke((Action) delegate {
    label3.Text = "Good: " + Good.ToString();
});

使用invoke函数将强制代码在与表单UI相同的线程上运行。