我使用另一个课程中的线程来更新标签,该标签是Winform Main类中的内容:
Scanner scanner = new Scanner(ref lblCont);
scanner.ListaFile = this.listFiles;
Thread trd = new Thread(new ThreadStart(scanner.automaticScanner));
trd.IsBackground = true;
trd.Start();
while (!trd.IsAlive) ;
trd.Join();
在第二类(扫描仪)中,我有一个名为“automaticScanner”的方法,它应该用下面的代码更新标签:
public Scanner(ref ToolStripStatusLabel _lblContatore)
{
lblCounter= _lblContatore;
}
Thread threadUpdateCounter = new Thread(new ThreadStart(this.UpdateCounter));
threadUpdateCounter.IsBackground = true;
threadUpdateCounter.Start();
while (!threadUpdateCounter .IsAlive) ;
threadUpdateCounter.Join();
private void AggiornaContatore()
{
this.lblCounter.Text = this.index.ToString();
}
我在更新标签时收到此错误:
跨线程操作无效:从其创建线程以外的线程访问控制'Main'
新闻:问题是这条线:
trd.Join();
这行禁止我的GUI和标签不更新。有方法来控制线程的完成并更新标签直到结束吗?
相似问题