dtb
dtb

Reputation: 217233

Create a completed Task<T>

I'm implementing a method Task<Result> StartSomeTask() and happen to know the result already before the method is called. How do I create a Task<T> that has already completed?

This is what I'm currently doing:

private readonly Result theResult = new Result();

public override Task<Result> StartSomeTask()
{
    var task = new Task<Result>(() => theResult);
    task.RunSynchronously(CurrentThreadTaskScheduler.CurrentThread);
    return task;
}

Is there a better solution?

Upvotes: 143

Views: 54938

Answers (6)

Daryl
Daryl

Reputation: 3724

For tasks with no return value, .NET 4.6 has added Task.CompletedTask.

It returns a task which is already in state TaskStatus.RanToCompletion. It probably returns the same instance every time, but the documentation warns you not to count on that fact.

Upvotes: 11

Atif Rehman
Atif Rehman

Reputation: 447

You can try var myAlreadyCompletedTask = Task.FromResult<string>("MyValue") This will give you a task with a specified return type

Upvotes: 1

zumalifeguard
zumalifeguard

Reputation: 9016

Calling Task.WhenAll without any parameters will return a completed task.

Task task = Task.WhenAll();

Upvotes: 1

CodesInChaos
CodesInChaos

Reputation: 108790

When targeting .NET 4.5 you can use Task.FromResult:

public static Task<TResult> FromResult<TResult>(TResult result);

To create a failed task, use Task.FromException:

public static Task FromException(Exception exception);
public static Task<TResult> FromException<TResult>(Exception exception);

.NET 4.6 adds Task.CompletedTask if you need a non generic Task.

public static Task CompletedTask { get; }

Workarounds for older versions of .NET:

  • When targeting .NET 4.0 with Async Targetting Pack (or AsyncCTP) you can use TaskEx.FromResult instead.

  • To get non-generic Task prior to .NET 4.6, you can use the fact that Task<T> derives from Task and just call Task.FromResult<object>(null) or Task.FromResult(0).

Upvotes: 229

Niall Connaughton
Niall Connaughton

Reputation: 16107

If you're using Rx, an alternative is Observable.Return(result).ToTask().

Upvotes: 2

QrystaL
QrystaL

Reputation: 4966

private readonly Result theResult = new Result();

public override Task<Result> StartSomeTask()
{
    var taskSource = new TaskCompletionSource<Result>();
    taskSource.SetResult(theResult);
    return taskSource.Task;
}

Upvotes: 117

Related Questions