I found this two implementation for "DoEvents" method:
SOLUTION 1:
System.Windows.Application.Current.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Background,
new System.Threading.ThreadStart(() => { }));
SOLUTION 2:
System.Windows.Application.Current.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Background,
new System.Action(delegate { }));
Could you explain what is the difference between these two implementations, and what is the most appropriate to use?
Thanks.
There is no difference between both solutions except the syntax. ThreadStart
and Action
are both delegates which have the same declaration and only a name is different:
public delegate void ThreadStart();
public delegate void Action();
You can also create your own delegate and use is in the same way e.g.:
public delegate void MyOwnAction();
...
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Background, new MyOwnAction(() => { }));
You can also use a specific method and not anonymous one:
private void Target()
{
...
}
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Background, new MyOwnAction(Target));