相关疑难解决方法(0)

在TPL中返回一个空的静态任务是一种不好的做法?

有些情况下我想要有条件地运行任务.我使用某种类似的扩展方法:

public static class MyTaskExtension{
  private static Task theEmptyTask = Task.Factory.StartNew(() => {}); //This is the question

  public static Task ContinueWith(this Task task, Task continuationTask, Func<bool> condition)
  {
    if condition(){
       ... do the work
    }
    return theEmptyTask;
  }
}
Run Code Online (Sandbox Code Playgroud)

我的期望是,theEmptyTask已经完成,所以基本上如果我不想做任何事情我只返回这个任务,而不是null或一个新的空任务.

我觉得这种方法应该有一些小问题.有人能看到吗?

c# static task task-parallel-library

15
推荐指数
1
解决办法
5789
查看次数

标签 统计

c# ×1

static ×1

task ×1

task-parallel-library ×1