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