runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Non value returning TaskCompletionSource

Open benaadams opened this issue 10 years ago • 6 comments

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();
}

benaadams avatar May 24 '16 17:05 benaadams

We need formal API proposal.

karelz avatar Oct 13 '16 22:10 karelz

@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!

stephentoub avatar Feb 25 '20 15:02 stephentoub

Strange thought I had, added

benaadams avatar Feb 26 '20 04:02 benaadams

Should it be SetResult() and TrySetResult() even though we will not be setting a result? Couldn't it just be Complete() and TryComplete()?

neville-nazerane avatar Mar 01 '20 10:03 neville-nazerane

Approximately what the change would look like: https://github.com/stephentoub/runtime/commit/6045033aef9e5fc7795017c788cbebe3744595bf

stephentoub avatar Mar 05 '20 20:03 stephentoub

Video

Looks good as proposed.

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();
    }
}

terrajobst avatar Jun 04 '20 17:06 terrajobst