有人知道两者的区别吗
Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(() =>
{
和
Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
应该没有区别。ThreadStart
并Action
定义为
public delegate void ThreadStart();
public delegate void Action();
即,没有参数也没有返回值的委托。所以它们在语义上是相同的。
但是,我会使用Action
and not ThreadStart
,因为它与构造函数ThreadStart
密切相关,因此带有的代码可以提示直接创建线程,因此会产生轻微的误导。Thread
ThreadStart
看起来和在上下文中是有区别的。ThreadStart
Action
BeginInvoke
正如 Vlad 所提到的,它们都将在委托中正确运行代码。
但是,如果委托中发生异常,则会ThreadStart
导致TargetInvocationException
. 但是 usingAction
会为您提供来自委托的正确异常。
Action
出于这个原因,应该首选。
看看这个问题。