METHOD

TaskCompletionSource<T>.TrySetResult

Attempts to complete a pending task successfully. Returns false if the task is already terminal. It never replaces a previously established result or terminal state.

Formal Syntax & Structure

csharp syntaxcsharp
source.TrySetResult(result)

Working Example

Example Snippetcsharp
using System;using System.Threading;using System.Threading.Tasks;var gate = new TaskCompletionSource<int>(); Console.WriteLine(gate.TrySetResult(3)); Console.WriteLine(gate.TrySetResult(7)); // True then False
C# & .NET Reference