我发现了"DoEvents“方法的两个实现:
解决方案1:
System.Windows.Application.Current.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Background,
new System.Threading.ThreadStart(() => { }));解决方案2:
System.Windows.Application.Current.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Background,
new System.Action(delegate { }));您能否解释一下这两种实现之间的区别,以及最适合使用的是什么?
谢谢。
发布于 2014-11-07 10:44:48
除了语法之外,这两种解决方案之间没有区别。ThreadStart和Action都是具有相同声明的委托,只有一个名称是不同的:
public delegate void ThreadStart();
public delegate void Action();您还可以创建自己的委托,并以相同的方式使用,例如:
public delegate void MyOwnAction();
...
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Background, new MyOwnAction(() => { }));您还可以使用特定的方法而不是匿名的方法:
private void Target()
{
...
}
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Background, new MyOwnAction(Target));发布于 2014-11-07 10:20:35
它们都是委托,当您的事件执行需要完成时,它将在您的条件满足时启动事件(例如,在达到执行时和到达dispatcher优先级背景时)--它们只是两种不同的实现方式。
What is the difference between Delegate & Action in C#
或msdn以获取信息
http://msdn.microsoft.com/en-us/library/system.threading.threadstart(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.action(v=vs.110).aspx
https://stackoverflow.com/questions/26798859
复制相似问题