C#-程序冻结
我制作了代理检查器,出于某种原因,当我点击“检查”按钮时,程序会冻结,直到工作完成 我尝试将代码移动到Backgroundworker:C#-程序冻结,c#,multithreading,C#,Multithreading,我制作了代理检查器,出于某种原因,当我点击“检查”按钮时,程序会冻结,直到工作完成 我尝试将代码移动到Backgroundworker: private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { WebProxy myproxy; foreach (string proxy in Proxies) { try
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:'跨线程操作无效:
控件“richTextBox1”是从其所在线程以外的线程访问的
是在上创建的。”
所以我试图删除label3.Text和label4.Text。
但是当我点击“检查”按钮时,我发现什么也没有发生
所以我尝试添加MessageBox.Show(“一些文本”)来查看它是否工作,我看到了MessageBox,但看起来线程跳过了检查
我能做什么
“检查”按钮代码:
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:表单
{
WebClient X=新的WebClient();
字符串url=“”;
ArrayList代理=新的ArrayList();
int TimeOut=3000,Good=0,Bad=0;
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
尝试
{
if(radioHTTP.Checked)
{
byte[]proxies=X.DownloadData(新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}\\d{1,6}”);
foreach(匹配m中的m)
richTextBox1.AppendText(m.Value.Replace(“,”:”)+“\r\n”);
}
如果(已选中无线电锁定)
{
byte[]proxies=X.DownloadData(新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}\\d{1,6}”);
foreach(匹配m中的m)
richTextBox1.AppendText(m.Value.Replace(“,”:”)+“\r\n”);
}
如果(已选中)
{
byte[]proxies=X.DownloadData(新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}\\d{1,6}”);
foreach(匹配m中的m)
richTextBox1.AppendText(m.Value.Replace(“,”:”)+“\r\n”);
}
如果(radioCustom.Checked)
{
byte[]proxies=X.DownloadData(新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}\\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}\\D{2,6}”);
foreach(A中匹配m)
richTextBox1.AppendText(m.Value.Replace(“,”:”)+“\r\n”);
foreach(在B中匹配m)
richTextBox1.AppendText(m.Value+“\r\n”);
foreach(匹配C中的m)
richTextBox2.AppendText(m.Value.Replace(“\”,““端口\”:\”,“:”+“\r\n”);
foreach(匹配D中的m)
richTextBox2.AppendText(m.Value.Replace(“,”:”)+“\r\n”);
}
string[]lines=richTextBox1.lines;
foreach(行中的字符串行)
代理。添加(行);
label1.Text=“行:”+Proxies.Count;
}抓住
{
}
}
私有无效按钮4_单击(对象发送者,事件参数e)
{
label1.Text=“行:0”;
richTextBox1.Clear();
backgroundWorker1.RunWorkerAsync();
}
私有void backgroundWorker1\u DoWork(对象发送方,DoWorkEventArgs e)
{
//backgroundWorker1.ReportProgress(0,“正在工作…”);
WebProxy-myproxy;
foreach(代理中的字符串代理)
{
尝试
{
myproxy=新的WebProxy(代理);
WebRequest=WebRequest.Create(“https://www.google.co.il/");
request.Timeout=超时;
Proxy=myproxy;
WebResponse re=request.GetResponse();
richTextBox1.AppendText(代理+“\r\n”);
好++;
调用((操作)委托{
label3.Text=“Good:+Good.ToString();
});
}
捕获(例外)
{
坏++;
调用((操作)委托{
label4.Text=“坏:”+Good.ToString();
});
}
}
//背景工作1.报告进度(100,“完成”);
}
非常感谢WinForms控件不能在创建它们的线程之外进行修改 这样做:
label3.Text = "Good: " + Good.ToString();
将在后台工作线程中失败,因为后台工作线程任务在单独的线程中运行
要解决此问题,可以在窗体线程上调用委托,如下所示:
this.Invoke((Action) delegate {
label3.Text = "Good: " + Good.ToString();
});
使用该函数将强制代码在与表单UI相同的线程上运行。您似乎试图从另一个线程访问UI元素。我试图使其在单独的线程上运行,但一直失败。我尝试了thread thread=new thread(method);thread.Start():我也遇到了同样的问题考虑使用和事件。BackgroundWorker类有一个
ProgressChanged
事件是有原因的。这实际上是我忘记了ProgressChanged
的一个好问题,现在我想我的答案更像是一个幼稚的方法。但是当我单击“检查”按钮时,什么都没有