Is it possible to force a task to execute synchronously, on the current thread?
That is, is it possible, by e.g. passing some parameter to StartNew()
, to ma
Since you mention testing, you might prefer using a TaskCompletionSource
Return a completed task with a result:
var tcs = new TaskCompletionSource();
tcs.SetResult(func());
return tcs.Task;
Return a faulted task:
var tcs = new TaskCompletionSource();
tcs.SetException(new InvalidOperationException());
return tcs.Task;
Return a canceled task:
var tcs = new TaskCompletionSource();
tcs.SetCanceled();
return tcs.Task;