Kermit
Kermit

Reputation: 34063

no definition for dispatcher

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.

Upvotes: 0

Views: 12020

Answers (2)

undefined
undefined

Reputation: 1364

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

Upvotes: 9

JSJ
JSJ

Reputation: 5691

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.

Upvotes: 1

Related Questions