【问题标题】:Difference between ThreadStart and ActionThreadStart 和 Action 的区别
【发布时间】:2015-02-26 23:27:42
【问题描述】:

有没有人知道

Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(() =>
{

Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{

【问题讨论】:

标签: c# .net wpf


【解决方案1】:

应该没有区别。 ThreadStartAction 定义为

public delegate void ThreadStart();

public delegate void Action();

即没有参数也没有返回值的委托。所以它们在语义上是相同的。


但是,我会使用Action 而不是ThreadStart,因为ThreadStartThread 构造函数密切相关,因此带有ThreadStart 的代码可以暗示直接创建线程,因此会产生轻微的误导。

【讨论】:

    【解决方案2】:

    BeginInvoke 的上下文中,ThreadStartAction 之间似乎存在 区别。

    正如 Vlad 所提到的,它们都将在委托中正确运行代码。

    但是,如果委托中发生异常,ThreadStart 将导致 TargetInvocationException。但是使用 Action 会给您来自委托的正确异常。

    出于这个原因,应该首选Action

    看看this question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 2012-02-08
      相关资源
      最近更新 更多