簡體   English   中英

從非UI線程更新Windows窗體上的標簽?

[英]Update label on windows form from non-UI thread?

我已經嘗試了2天了。 我查看了成堆的stackoverflow答案並嘗試了所有答案,但我仍然遇到相同的問題。

我在Windows窗體上有一個標簽。 Windows窗體上的唯一代碼是:

var thread1 = new Thread(new ThreadStart(Censored.Signup));
thread1.Start();

現在,在Windows窗體上,我也有一個標簽。 它的名字是“ createdLabel”

在我剛才談到的thread1(Censored.Signup)上,我有以下代碼(以及與實際上組成程序的其他代碼無關):

int created = 0;
for (int i = 0; i < 10; i++)
{

   created++;
   createdLabel.Text = (string.Format("Created: [{0}]", created));
}

我要做的就是使用工作線程(Censored.Signup)中的信息(創建的)來更新標簽(createdLabel,在窗體上)。

例如(“>”代表下一個/之后發生的事情):
label1 = 0>在被檢查的循環中,注冊運行並更新表單> label1 = 1上的標簽

我嘗試將其解釋為“像我5歲時一樣解釋”,以使其變得真正清晰,但是如果有人感到困惑並需要清除任何內容,請告訴我。 任何幫助將不勝感激。 謝謝!

標簽的內容只能從UI線程更改。 使用Invoke方法在具有正確文本的UI線程上執行回調

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM