[英]Status Label not updating on background worker progress update
以前我曾見過類似的問題,但似乎沒有一個適合我的情況。 我有一個Windows窗體,該窗體有一個后台工作程序,該工作程序支持更新。 我有一個狀態條標簽。
通過單擊按鈕啟動后台工作程序,單擊該按鈕時,后台工作程序開始正常運行。 每次迭代我都會進行進度更新,並以用戶狀態發送字符串。 如果我使用該字符串來更新表單的標題,則標題會隨着過程的運行而變化,並且可以按預期方式工作...但是,如果我嘗試使用該字符串來更新狀態條標簽的值,則標簽沒有更新。
我正在UI線程之外的單獨線程上進行工作,而且無論如何我都不會阻塞UI線程,這可以通過按預期觀看標題更新來證明。 但是,狀態條標簽未更新。
顯然,狀態條標簽不是System.Windows.Controls的子類,但是我試圖使狀態條標簽無效,並且我試圖使狀態條標簽所在的狀態條無效並刷新,但是我不能獲取狀態欄標簽以在后台工作時更改文本。
這是Progress Changed事件的代碼
void workerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//_statusCurrentActivityLBL.Text = e.UserState.ToString();
String s = e.UserState.ToString();
Text = s;
_statusCurrentActivityLBL.Text = s;
_statusCurrentActivityLBL.Invalidate();
statusStrip1.Invalidate();
statusStrip1.Refresh();
}
如我所說,Windows窗體文本正確更改,但狀態標簽沒有更改。
UPDATE @Richard Schneider Label的類名稱為ToolStripStatusLabel它似乎沒有Refresh()成員。
更新2 @Chibueze Opata代碼運行無錯誤。 如果我更新簡單標簽控件上的文本,標題文本將發生變化(這是我當前的解決方法)。 問題在於狀態條上的ToolStripStatusLabel無法更新。
在更新標簽之前嘗試使用Application.DoEvents()
試試這個
Delegate Sub SetLabelText(ByVal [Label] As Label, ByVal [text] As String)
Private Sub SetStatus(ByVal [Label] As Label, ByVal [text] As String)
' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If [Label].InvokeRequired Then
Dim MyDelegate As New SetLabelText(AddressOf SetStatus)
Me.Invoke(MyDelegate, New Object() {[Label], [text]})
Else
[Label].Text = [text]
End If
End Sub
像這樣調用此函數
SetStatus(lblStatus,"Status message")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.