I want to create a completed Task (not Task). Is there something built into .NET to do this?
A related question: Create a complete
Task is implicitly convertable to Task, so just get a completed Task (with any T and any value) and use that. You can use something like this to hide the fact that an actual result is there, somewhere.
private static Task completedTask = Task.FromResult(false);
public static Task CompletedTask()
{
return completedTask;
}
Note that since we aren't exposing the result, and the task is always completed, we can cache a single task and reuse it.
If you're using .NET 4.0 and don't have FromResult then you can create your own using TaskCompletionSource:
public static Task FromResult(T value)
{
var tcs = new TaskCompletionSource();
tcs.SetResult(value);
return tcs.Task;
}