Reputation: 319
Hi guys i am switching one of my projects from windowsforms to WPF. So far i used this piece of code to invoke my GUI objects with data from another thread:
this.Invoke(new Action<NewDiagPacketArrivedEventArgs>(DoSomething), e);
How can I do the same thing in WPF? I found a few posts about using a dispatcher, but I could not get it to work. I would like to have a simple solution. If you know any guides on this topic I would appreciate it.
Thanks
Upvotes: 2
Views: 6009
Reputation: 138
In Winforms, we use Control.Invoke to marshal the code from other thread to GUI thread. In WPF its Dispatcher.Invoke, every Dispatcher object have its own Dispatcher, on thats queue it will be marshaled.
Dispatcher.BeginInvoke(new Action(() =>
{
//Your code
}));
Upvotes: 2
Reputation: 116
Try this
this.Dispatcher.Invoke(()=>DoStuffOnGUIThread());
DoStuffInGUIThread() is the method that contains the code you want to execute on the UI thread.
Upvotes: 7