Runtime: Non value returning TaskCompletionSource

Created on 24 May 2016  路  6Comments  路  Source: dotnet/runtime

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();
}
api-approved area-System.Threading.Tasks

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

All 6 comments

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()?

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sahithreddyk picture sahithreddyk  路  3Comments

chunseoklee picture chunseoklee  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments

jchannon picture jchannon  路  3Comments

nalywa picture nalywa  路  3Comments