问题描述
我一直在尝试2天来做到这一点。我已经看过吨堆栈溢出的答案,并尝试所有这些,我仍然遇到同样的问题。
我在Windows窗体上有一个标签。这个窗体上唯一的代码是:
pre $ var $ thread $ = new Thread(new ThreadStart(Censored.Signup));
thread1.Start();
现在,在窗体窗体上我也有一个标签。它的名字是createdLabel
在我刚才谈到的thread1(Censored.Signup)上,我有以下代码(沿着其他代码无关紧要):
int created = 0;
for(int i = 0; i {
created ++;
createdLabel.Text =(string.Format(Created:[{0}],created));
}
我要的是更新标签(createdLabel,在窗体上)工作线程(Censored.Signup)的信息(创建)。例如(>表示下一个/之后会发生什么):
label1 = 0> Censored.Signup中的循环遍历并更新标签在表单上> label1 = 1
我尝试将此解释为尽可能地解释我的5,以便尽量清楚,但如果有人感到困惑,需要清理任何事情只要让我知道。任何帮助将不胜感激。谢谢!
标签的内容只能从UI线程中更改。使用 Invoke
方法在具有正确文本的UI线程上执行回调
string text = string.Format(Created:[{0}],created);
createdLabel.Invoke((Action)delegate {createdLabel.Text = text;});
I've been trying for 2 days to do this. I've looked at tons of stackoverflow answers and tried all of them, i still get the same problem.
I have a label on a windows form. The only code on this windows form is:
var thread1 = new Thread(new ThreadStart(Censored.Signup));
thread1.Start();
Now, on the windows form I also have a label. It's name is "createdLabel"
On the thread1 (Censored.Signup) i talked about a minute ago, I have the following code (along w/ other code that actually makes up the program, irrelevant):
int created = 0;
for (int i = 0; i < 10; i++)
{
created++;
createdLabel.Text = (string.Format("Created: [{0}]", created));
}
All i want is to update the label (createdLabel, on the form) with the info (created) from the worker thread (Censored.Signup).
for example (">" represents what happens next/after):
label1 = 0 > loop in Censored.Signup runs through and updates label on form > label1 = 1
I tried to make this as "explain like i'm 5" as possible to try and be really clear, but if anyone is confused and needs anything cleared up just let me know. Any help would be greatly appreciated. Thanks!
The contents of the label can only be changed from the UI thread. Use the Invoke
method to execute a call back on the UI thread that has the correct text
string text = string.Format("Created: [{0}]", created);
createdLabel.Invoke((Action)delegate { createdLabel.Text = text; });
这篇关于从非UI线程更新windows窗体上的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!