0

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.

Kermit
  • 33,827
  • 13
  • 85
  • 121

2 Answers2

9

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

undefined
  • 1,354
  • 1
  • 8
  • 19
  • 1
    This throws 3 errors; `The best overloaded method match for 'System.Windows.Forms.Control.BeginInvoke(System.Delegate, params object[])' has some invalid arguments`, `cannot convert from 'System.Windows.Threading.DispatcherPriority' to 'System.Delegate'` and `cannot convert from 'System.Threading.ThreadStart' to 'object[]'` – Kermit Apr 23 '13 at 14:42
  • there is no `DispatcherPriority` in windorms. Just pass a delegate. I provided a code example in answer – undefined Apr 23 '13 at 14:44
  • 1
    the `=>` is throwing an error: `Invalid expression term '=>'` – Kermit Apr 23 '13 at 14:45
  • Updated it to vanilla .net 2.0 style. which version of .net framework are you using? – undefined Apr 23 '13 at 14:48
  • I am working on 4.0. Your latest edit worked great. Thank you. – Kermit Apr 23 '13 at 14:51
1

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.

JSJ
  • 5,653
  • 3
  • 25
  • 32