9

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?

Amittai Shapira
  • 3,749
  • 1
  • 30
  • 54
vklu4itesvet
  • 375
  • 1
  • 4
  • 15

2 Answers2

15

you can pass this:

new Action(GetStatusList)

or

(Action)(() => { GetStatusList; })
Mohamed Abed
  • 5,025
  • 22
  • 31
7

You can avoid explicit casting by creating a simple method:

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

Use this as follows:

RunInUiThread(() =>
{
     GetStatusList();
});
gls123
  • 5,467
  • 2
  • 28
  • 28
  • Hi, i have 2 problems with your sample code. first, last line has a missing ')'. Second if i try your example i get an error, that for the non static field Dispatcher.Invoke(System.Action) a object reference is needed. – Offler Apr 22 '13 at 06:47
  • Thanks I have applied correction to the above. Dispatcher is a non-static property on a DispatcherObject, which is the lowest base class of Control, Window, FrameworkElement etc. So you can only use Dispatcher in non-static contexts. – gls123 Apr 22 '13 at 14:02