如何在另一个标签中显示每个线程
本文关键字:显示 线程 标签 另一个 | 更新日期: 2023-09-27 18:04:19
在button1 click事件中,我创建了一些后台工作者,这取决于我在列表框中选择了多少项:
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);
}
}
问题是,在标签4。文本我将看到两个线程的最后一个URL。比如,我想要两个不同的标签。每个线程都会显示另一个线程。例如,在label4中。文本将在www.cnn.com和label7中。文本将为www.cnet.com.
我不明白如何捕获或获得每个线程URL并将其放在标签中?
是的,首先我建议重命名您的控件-正如@Henk_Holterman建议的那样。例如,您不仅可以传递输入作为参数,还可以传递一些要更新的带有input + label的类/对。然后,更新委托以使用指定的标签。