Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non value returning TaskCompletionSource #17393

Closed
benaadams opened this issue May 24, 2016 · 6 comments · Fixed by #37452
Closed

Non value returning TaskCompletionSource #17393

benaadams opened this issue May 24, 2016 · 6 comments · Fixed by #37452
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Threading.Tasks
Milestone

Comments

@benaadams
Copy link
Member

benaadams commented May 24, 2016

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();
}
@stephentoub stephentoub removed their assignment Sep 30, 2016
@karelz
Copy link
Member

karelz commented Oct 13, 2016

We need formal API proposal.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 5.0 milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@stephentoub
Copy link
Member

@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 stephentoub removed the untriaged New issue has not been triaged by the area owner label Feb 25, 2020
@stephentoub stephentoub modified the milestones: 5.0, Future Feb 25, 2020
@benaadams
Copy link
Member Author

Strange thought I had, added

@neville-nazerane
Copy link

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

@stephentoub stephentoub added api-ready-for-review and removed api-needs-work API needs work before it is approved, it is NOT ready for implementation labels Mar 5, 2020
@stephentoub
Copy link
Member

Approximately what the change would look like:
stephentoub@6045033

@terrajobst terrajobst added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review labels Jun 4, 2020
@terrajobst
Copy link
Member

terrajobst commented Jun 4, 2020

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Threading.Tasks
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants