no definition for dispatcher

耗尽温柔 提交于 2019-12-23 05:23:54

问题


I'm getting the error

'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

From

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

According to the documentation, the Dispatcher class is part of the namespace System.Windows.Threading, which I'm using.

Am I missing another reference?

In case it's relevant, I added this after receiving an error that "cross-thread operation was not valid" using a server/client socket.


回答1:


WinForms does not have a Dispatcher in it.

In order to post asynchronous UI update( that's exactly what Dispatcher.BeginInvoke does), just use this.BeginInvoke(..) It's a method from Control base class. In your case you could have something like this (adopted from 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;
}

If you want a synchronous update, use this.Invoke




回答2:


The Dispatcher concept belong to WPF technology and you are using Winforms on winforms you can use this or control .Begin or BeginInvoke both of these are similer to Dispatcher.Begin or Dispatcher.BeginInvoke

Basically both of these are from Delegate class which is getting implemented by CLR for you at runtime.



来源:https://stackoverflow.com/questions/16172078/no-definition-for-dispatcher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!