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

前端 未结 2 1415
渐次进展
渐次进展 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)).

    0 讨论(0)
  • 2021-02-13 06:36

    It's perfectly acceptable to return an already completed task in some contexts. It's not something that is done particularly often, but it is done.

    There's also nothing wrong at all with just using a single static completed task. There is no need to have a whole bunch of different tasks that are all identical, given that once they're completed, and if they have no result, there's nothing wrong with reusing them.

    Note that if you want to return an already completed task you can use Task.FromResult to generate one with less overhead than what you're doing now, as you won't be creating an empty method, scheduling it, waiting for it to be started, and then have it finish right away. Just returning Task.FromResult(false) will give you an already completed task.

    If you are using .NET 4.0 you can create your own FromResult easily enough:

    public static Task FromResult<T>(T result)
    {
        var tcs = new TaskCompletionSource<T>();
        tcs.SetResult(result);
        return tcs.Task;
    }
    
    0 讨论(0)
提交回复
热议问题