2013-04-23 49 views
-2

沒有定義,我發現了錯誤調度員

'socketServer.Form1' does not contain a definition for 'Dispatcher' and no extension method 'Dispatcher' accepting a first argument of type 'socketServer.Form1' could be found

private void tbAux_SelectionChanged(object sender, EventArgs e) 
{ 
    this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate() 
    { 
     textBox.Text = tbAux.Text; 
    } 
    ); 
} 

按照documentation,該Dispatcher類是命名空間System.Windows.Threading,我使用的是哪一部分。

我是否缺少另一個參考?

萬一它是相關的,我在使用服務器/客戶端套接字接收到「跨線程操作無效」的錯誤後添加了這個。

+0

我想你想拋棄this.Dispatcher的'this.'部分,但我不積極。 – 2013-04-23 14:35:17

+0

@ChrisSinclair試過; 「對象引用是必需的」 – Kermit 2013-04-23 14:36:04

+0

這是WPF還是WinForms? – 2013-04-23 14:36:22

回答

4

WinForms在其中沒有Dispatcher

爲了發佈異步UI更新(這正是Dispatcher.BeginInvoke所做的),只需使用this.BeginInvoke(..)這是一個基於Control基類的方法。 你的情況,你可以有這樣的事情(從MSDN pattern通過):

private delegate void InvokeDelegate(); 
private void tbAux_SelectionChanged(object sender, EventArgs e) 
{ 
    this.BeginInvoke(new InvokeDelegate(HandleSelection)); 
} 
private void HandleSelection() 
{ 
    textBox.Text = tbAux.Text; 
} 

如果你想有一個同步更新,使用this.Invoke

+0

這會引發3個錯誤; ''System.Windows.Forms.Control.BeginInvoke(System.Delegate,params object [])'的最佳重載方法匹配有一些無效參數,''無法從'System.Windows.Threading.DispatcherPriority'轉換爲'System .Delegate''和''無法從'System.Threading.ThreadStart'轉換爲'object []'' – Kermit 2013-04-23 14:42:32

+0

在風蟲中沒有'DispatcherPriority'。只需通過一個代表。我在回答 – undefined 2013-04-23 14:44:33

+1

中提供了一個代碼示例,'=>'拋出一個錯誤:'無效的表達式'=>' – Kermit 2013-04-23 14:45:47

0

Dispatcher概念屬於WPF技術和你使用的WinForms winforms你可以使用這個或控制.Begin或BeginInvoke這兩個都是模擬到Dispatcher.Begin或Dispatcher.BeginInvoke

基本上這兩個都是從委託類CLR爲您在運行時實現。