Skip to content

Non value returning TaskCompletionSource #17393

@benaadams

Description

@benaadams

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions