2013-04-23 70 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为您在运行时实现。