2013-08-27 28 views
1

在線程中我想改變標籤的值Dispatcher.Invoke不改變標籤的值

我發現我必須使用Dispatcher.Invoke。

嘗試這種代碼:

Me.Dispatcher.Invoke(DispatcherPriority.Background, (Function() Me.Label2.Content = "Scanning done.")) 

,但它不會改變的價值。

我在做什麼錯?

@edit:我的目標框架是4.0

+0

多久你久等了。 BackgroundPriority不會立即改變... –

+0

我等了大約10秒,但我試圖改變背景其他優先事項。仍然沒有改變。 – Disa

回答

2

中最突出的問題是您使用的是Function,你應該使用一個Sub因爲沒有返回值。另外,你的參數是向後的。

以下應爲你工作:

Me.Dispatcher.Invoke(Sub() Me.Label2.Content = "Scanning done.", DispatcherPriority.Background) 
+0

我的代碼是寫在VB.NET – Disa

+0

但這:'Label2.Dispatcher.Invoke(DispatcherPriority.Background,(功能()Label2.Content = 「掃描完成。」))'無法正常工作或 – Disa

+0

請參閱編輯VB.NET。 – Khan

0

試試這個(.NET 4.5,在C#):

Application.Current.Dispatcher.InvokeAsync(() => Label2.Content = "Scanning done."); 
+0

他們要求在VB.NET中的語法。我相應地更新了問題中的標籤。 – Khan