Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MethodInvoker in WPF?

Tags:

.net

winforms

wpf

Is there any in-built delegate like System.Windows.Forms.MethodInvoker for WPF ?

I was porting some functionality from WinForms to WPF, for this I was not wising to import winforms reference, is there any corresponding delegate in WPF ?

like image 1000
Brij Avatar asked Apr 24 '13 09:04

Brij


3 Answers

You can use just like this;

Dispatcher.BeginInvoke(new Action(delegate
  {
    // Do your work
  }));
like image 105
Mehmet Ince Avatar answered Nov 09 '22 11:11

Mehmet Ince


MethodInvoker is used in WPF also. And you can use it in Dispatcher.Invoke or Control.Invoke methods. Also you can use Action delegate which has many overridings. They both will be efficient.

like image 29
Alex Avatar answered Nov 09 '22 11:11

Alex


For example, you have button control btnLogin you can use it like this:

In WPF:

btnLogin.Dispatcher.Invoke(new Action(delegate
{
    // Running on the UI thread
    btnLogin.Content = "TEST";
}));

In Winform:

btnLogin.Invoke((MethodInvoker)delegate {
    // Running on the UI thread
    btnLogin.Text = "TEST";
});

Cheers!

like image 1
Jian Zhong Avatar answered Nov 09 '22 12:11

Jian Zhong