C# 是否从非UI线程更新windows窗体上的标签?

C# 是否从非UI线程更新windows窗体上的标签?,c#,multithreading,winforms,forms,C#,Multithreading,Winforms,Forms,我已经试了两天了。我已经看了大量的stackoverflow答案,并尝试了所有答案,但我仍然遇到同样的问题 我在windows窗体上有一个标签。此windows窗体上的唯一代码是: var thread1 = new Thread(new ThreadStart(Censored.Signup)); thread1.Start(); 现在,在windows窗体上,我还有一个标签。它的名字是“createdLabel” 在我一分钟前提到的thread1(censtered.Signup)中,我有

我已经试了两天了。我已经看了大量的stackoverflow答案,并尝试了所有答案,但我仍然遇到同样的问题

我在windows窗体上有一个标签。此windows窗体上的唯一代码是:

var thread1 = new Thread(new ThreadStart(Censored.Signup));
thread1.Start();
现在,在windows窗体上,我还有一个标签。它的名字是“createdLabel”

在我一分钟前提到的thread1(censtered.Signup)中,我有以下代码(以及其他实际上构成程序的代码,与此无关):

int created=0;
对于(int i=0;i<10;i++)
{
创建++;
createdLabel.Text=(string.Format(“Created:[{0}]”,Created));
}
我只想用工作线程(censtered.Signup)中的信息(createdLabel,在表单上)更新标签(createdLabel)

例如(“>”表示接下来/之后发生的事情):
label1=0>循环处于已审查状态。注册运行并更新表单>label1=1上的标签


我试着尽可能地把这句话解释成“像我5岁一样解释”,试图说清楚,但如果有人感到困惑,需要澄清什么,请告诉我。任何帮助都将不胜感激。谢谢

标签的内容只能从UI线程更改。使用
Invoke
方法对具有正确文本的UI线程执行回调

string text = string.Format("Created: [{0}]", created);
createdLabel.Invoke((Action)delegate { createdLabel.Text = text; });

标签的内容只能从UI线程更改。使用
Invoke
方法对具有正确文本的UI线程执行回调

string text = string.Format("Created: [{0}]", created);
createdLabel.Invoke((Action)delegate { createdLabel.Text = text; });

当然,另一种解决方案是使用
BackgroundWorker
。当然,另一种解决方案是使用
BackgroundWorker