is returning an empty static task in TPL a bad practice?

前端 未结 2 1432
渐次进展
渐次进展 2021-02-13 05:40

There are cases that I would want to run a task conditionally. I use some sort of extension method like this:

public static class MyTaskExtension{
  private stat         


        
2条回答
  •  面向向阳花
    2021-02-13 06:32

    As long as you hand back a task that is in the completed state (use TaskCompletionSource to do this), I can't think of any problem with this since there aren't really any setters on the Task class that would allow the client to muck with your static empty task. They could call Dispose() on your task, but I don't think that would cause any harm (i.e. I don't think it would affect the ability to inspect the properties of the Task (haven't tried it out--something worth testing out)).

提交回复
热议问题