C# 从另一个线程更新标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14890295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update label from another thread
提问by Antonio
I use a thread writing in another class for update a label. The label is contents in Winform Main class.
我使用另一个类中的线程来更新标签。标签是 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();
How you can see, i pass the reference of label into constructor of the second class. In the second class(Scanner) i've a method called "automaticScanner" that should update the label with this code:
你怎么看,我将标签的引用传递给第二个类的构造函数。在第二个类(扫描仪)中,我有一个名为“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();
}
I've receive this error on update of label:
我在更新标签时收到此错误:
Cross-thread operation not valid: Control 'Main' accessed from a thread other than the thread it was created on
跨线程操作无效:控制“Main”从创建它的线程以外的线程访问
I use .net 4 with Winform C#.
我在 Winform C# 中使用 .net 4。
Thanks a lot for answers.
非常感谢您的回答。
News: The problem is this line:
新闻:问题是这一行:
trd.Join();
This line block my GUI and the lable was not update. There are methods to control the finish of thread and updating the label until the end? Thanks
这条线挡住了我的 GUI,标签没有更新。有没有办法控制线程的完成并更新标签直到结束?谢谢
采纳答案by Igoy
You cannot update UI from any other thread other than the UI thread. Use this to update thread on the UI thread.
您不能从 UI 线程以外的任何其他线程更新 UI。使用它来更新 UI 线程上的线程。
private void AggiornaContatore()
{
if(this.lblCounter.InvokeRequired)
{
this.lblCounter.BeginInvoke((MethodInvoker) delegate() {this.lblCounter.Text = this.index.ToString(); ;});
}
else
{
this.lblCounter.Text = this.index.ToString(); ;
}
}
Please go through this chapter and more from this book to get a clear picture about threading:
请阅读本章和本书中的更多内容,以清楚了解线程:
http://www.albahari.com/threading/part2.aspx#_Rich_Client_Applications
http://www.albahari.com/threading/part2.aspx#_Rich_Client_Applications
回答by Habib
Use MethodInvokerfor updating label text in other thread.
使用MethodInvoker更新其他线程中的标签文本。
private void AggiornaContatore()
{
MethodInvoker inv = delegate
{
this.lblCounter.Text = this.index.ToString();
}
this.Invoke(inv);
}
You are getting the error because your UI thread is holding the label, and since you are trying to update it through another thread you are getting cross thread exception.
您收到错误是因为您的 UI 线程持有标签,并且由于您尝试通过另一个线程更新它,因此您会遇到跨线程异常。
You may also see: Threading in Windows Forms
您可能还会看到:Windows 窗体中的线程