Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use lambda expressions as parameter in Dispatcher.Invoke()

I have such problem : there is some method

private List<int> GetStatusList()
        {
            return (List<int>)GetValue(getSpecifiedDebtStatusesProperty);
        }

To invoke it in main thread - I use

`delegate List<int> ReturnStatusHandler();` ...

this.Dispatcher.Invoke(new ReturnStatusHandler(GetStatusList));

How can I do the same, using lambda expression instead of custom delegate and method?

like image 291
vklu4itesvet Avatar asked Oct 10 '11 08:10

vklu4itesvet


2 Answers

you can pass this:

new Action(GetStatusList)

or

(Action)(() => { GetStatusList; })
like image 65
Mohamed Abed Avatar answered Oct 18 '22 15:10

Mohamed Abed


You can avoid explicit casting by creating a simple method:

void RunInUiThread(Action action)
{
     Dispatcher.Invoke(action);
}

Use this as follows:

RunInUiThread(() =>
{
     GetStatusList();
});
like image 7
gls123 Avatar answered Oct 18 '22 15:10

gls123