Reputation: 21
I made proxy checker, and for some reason when I click the "Check" button, the program just freeze until the work is done.
I tried to move the code to 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");
}
but for some reason I get this error:
System.InvalidOperationException: 'Cross-thread operation not valid: Control 'richTextBox1' accessed from a thread other than the thread it was created on.'
so I tried to remove the label3.Text and label4.Text. but then I saw that nothing happen when I click the "Check" Button.
so I tried to add MessageBox.Show("some text") to see if it's working, I saw the messagebox but it looks like the Thread skip the check.
what can I do?
"Check" button code:
label1.Text = "Lines: 0";
richTextBox1.Clear();
backgroundWorker1.RunWorkerAsync();
The full code:
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");
}
Thank you very much
Upvotes: 1
Views: 129
Reputation: 2482
WinForms controls cannot be modified outside of the thread they were created in.
So doing something like this:
label3.Text = "Good: " + Good.ToString();
Will fail in a background worker as background worker tasks are run in a separate thread.
To get around this you can invoke a delegate on the form's thread like so:
this.Invoke((Action) delegate {
label3.Text = "Good: " + Good.ToString();
});
Using the invoke function will force the code to run on the same thread as the form's UI.
Upvotes: 1