ThreadStart和Action之间的区别

Dev*_*per 10 .net c# wpf

有人知道之间的区别

Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(() =>
{
Run Code Online (Sandbox Code Playgroud)

Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
Run Code Online (Sandbox Code Playgroud)

Vla*_*lad 12

应该没有区别.ThreadStartAction定义为

public delegate void ThreadStart();

public delegate void Action();
Run Code Online (Sandbox Code Playgroud)

即,没有参数且没有返回值的委托.所以它们在语义上是一样的.


但是,我会使用Action而不是ThreadStart,因为ThreadStart它与Thread构造函数紧密相关,因此代码ThreadStart可以暗示引导线程创建,因此有点误导.


Chr*_*ill 5

它看起来像有之间的差异ThreadStart,并Action在上下文BeginInvoke.

正如弗拉德提到的那样,他们都会正确地在代表中运行代码.

但是,如果委托中发生异常,则会产生ThreadStarta TargetInvocationException.但是使用Action为代表提供了正确的例外.

Action 因此应该是首选.

看看这个问题.