TaskCompletionSource<TResult> is an easy to use class to use for deferred result returning Tasks controlled via user code; with coverage for exception and cancellation handling.
However it would be good to have a non-result returning, non-generic version also e.g. TaskCompletionSource with TrySetResult => TrySetCompleted
I often see TaskCompletionSource<object> with TrySetResult(null) used for this purpose, however that is not very clean.
public class TaskCompletionSource
{
public TaskCompletionSource();
public TaskCompletionSource(object? state);
public TaskCompletionSource(TaskCreationOptions creationOptions);
public TaskCompletionSource(object? state, TaskCreationOptions creationOptions);
public Task Task { get; }
public void SetCanceled();
public void SetException(IEnumerable<Exception> exceptions);
public void SetException(Exception exception);
public void SetResult();
public bool TrySetCanceled();
public bool TrySetCanceled(CancellationToken cancellationToken);
public bool TrySetException(IEnumerable<Exception> exceptions);
public bool TrySetException(Exception exception);
public bool TrySetResult();
}
We need formal API proposal.
@benaadams, thoughts on whether this is still important? If you believe it is, can you please add the API proposal details? If not, can you please close it? Thanks!
Strange thought I had, added
Should it be SetResult() and TrySetResult() even though we will not be setting a result?
Couldn't it just be Complete() and TryComplete()?
Approximately what the change would look like:
https://github.com/stephentoub/runtime/commit/6045033aef9e5fc7795017c788cbebe3744595bf
Looks good as proposed.
C#
namespace System.Threading.Tasks
{
public class TaskCompletionSource
{
public TaskCompletionSource();
public TaskCompletionSource(object? state);
public TaskCompletionSource(TaskCreationOptions creationOptions);
public TaskCompletionSource(object? state, TaskCreationOptions creationOptions);
public Task Task { get; }
public void SetCanceled();
public void SetException(IEnumerable<Exception> exceptions);
public void SetException(Exception exception);
public void SetResult();
public bool TrySetCanceled();
public bool TrySetCanceled(CancellationToken cancellationToken);
public bool TrySetException(IEnumerable<Exception> exceptions);
public bool TrySetException(Exception exception);
public bool TrySetResult();
}
}
Most helpful comment
Video
Looks good as proposed.
C# namespace System.Threading.Tasks { public class TaskCompletionSource { public TaskCompletionSource(); public TaskCompletionSource(object? state); public TaskCompletionSource(TaskCreationOptions creationOptions); public TaskCompletionSource(object? state, TaskCreationOptions creationOptions); public Task Task { get; } public void SetCanceled(); public void SetException(IEnumerable<Exception> exceptions); public void SetException(Exception exception); public void SetResult(); public bool TrySetCanceled(); public bool TrySetCanceled(CancellationToken cancellationToken); public bool TrySetException(IEnumerable<Exception> exceptions); public bool TrySetException(Exception exception); public bool TrySetResult(); } }