在 button1 单击事件中,我创建了许多后台工作人员,这取决于我在列表框中选择了多少项目:
private void button1_Click(object sender, EventArgs e)
{
// For each item selected in the listbox
foreach (var item in listBox1.SelectedItems)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += backgroundWorker1_DoWork;
worker.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
object argument = item;
worker.RunWorkerAsync(argument);
}
offlineOnline = false;
init();
button1.Enabled = false;
this.Text = "Processing...";
label6.Text = "Processing...";
label6.Visible = true;
button2.Enabled = false;
checkBox1.Enabled = false;
checkBox2.Enabled = false;
numericUpDown1.Enabled = false;
button3.Enabled = true;
button6.Enabled = false;
button4.Enabled = true;
button5.Enabled = false;
listBox1.Enabled = false;
}
}
然后在 DoWork 事件中,我使用这样的线程:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
object input = e.Argument;
string f = GetUrl(input);
this.Invoke(new MethodInvoker(delegate { label2.Text = "Website To Crawl: "; }));
this.Invoke(new MethodInvoker(delegate { label4.Text = f; }));
if (offlineOnline == true)
{
offlinecrawling(f, levelsToCrawl, e);
}
else
{
webCrawler(f, levelsToCrawl, e);
}
}
问题是在 label4.Text 中,我将看到来自两个线程的最后一个 URL。例如,我想要两个不同的标签。每个将显示另一个线程。例如,label4.Text 中将有 www.cnn.com,而 label7.Text 中将有 www.cnet.com。
我不明白如何捕获或获取每个线程 URL 并将其放入标签中?