/// Represents the producer side of a <see cref="System.Threading.Tasks.Task"/> unbound to a delegate, providing access to the consumer side through the <see cref="Task"/> property.
/// </summary>
publicsealedclassTaskCompletionSource
{
/// <summary>
/// The underlying TCS.
/// </summary>
privatereadonlyTaskCompletionSource<object>_tcs;
/// <summary>
/// Initializes a new instance of the <see cref="TaskCompletionSource"/> class.
/// </summary>
publicTaskCompletionSource()
{
_tcs=newTaskCompletionSource<object>();
}
/// <summary>
/// Initializes a new instance of the <see cref="TaskCompletionSource"/> class with the specified state.
/// </summary>
/// <param name="state">The state to use as the underlying <see cref="Task"/>'s <see cref="IAsyncResult.AsyncState"/>.</param>
publicTaskCompletionSource(objectstate)
{
_tcs=newTaskCompletionSource<object>(state);
}
/// <summary>
/// Initializes a new instance of the <see cref="TaskCompletionSource"/> class with the specified options.
/// </summary>
/// <param name="creationOptions">The options to use when creating the underlying <see cref="Task"/>.</param>
/// Gets the <see cref="Task"/> created by this <see cref="TaskCompletionSource"/>.
/// </summary>
publicTaskTask
{
get{return_tcs.Task;}
}
/// <summary>
/// Transitions the underlying <see cref="Task"/> into the <see cref="TaskStatus.Canceled"/> state.
/// </summary>
/// <exception cref="InvalidOperationException">The underlying <see cref="Task"/> has already been completed.</exception>
publicvoidSetCanceled()
{
_tcs.SetCanceled();
}
/// <summary>
/// Attempts to transition the underlying <see cref="Task"/> into the <see cref="TaskStatus.Canceled"/> state.
/// </summary>
/// <returns><c>true</c> if the operation was successful; otherwise, <c>false</c>.</returns>
publicboolTrySetCanceled()
{
return_tcs.TrySetCanceled();
}
/// <summary>
/// Transitions the underlying <see cref="Task"/> into the <see cref="TaskStatus.Faulted"/> state.
/// </summary>
/// <param name="exception">The exception to bind to this <see cref="Task"/>. May not be <c>null</c>.</param>
/// <exception cref="InvalidOperationException">The underlying <see cref="Task"/> has already been completed.</exception>
publicvoidSetException(Exceptionexception)
{
_tcs.SetException(exception);
}
/// <summary>
/// Transitions the underlying <see cref="Task"/> into the <see cref="TaskStatus.Faulted"/> state.
/// </summary>
/// <param name="exceptions">The collection of exceptions to bind to this <see cref="Task"/>. May not be <c>null</c> or contain <c>null</c> elements.</param>
/// <exception cref="InvalidOperationException">The underlying <see cref="Task"/> has already been completed.</exception>
/// Attempts to transition the underlying <see cref="Task"/> into the <see cref="TaskStatus.Faulted"/> state.
/// </summary>
/// <param name="exception">The exception to bind to this <see cref="Task"/>. May not be <c>null</c>.</param>
/// <returns><c>true</c> if the operation was successful; otherwise, <c>false</c>.</returns>
publicboolTrySetException(Exceptionexception)
{
return_tcs.TrySetException(exception);
}
/// <summary>
/// Attempts to transition the underlying <see cref="Task"/> into the <see cref="TaskStatus.Faulted"/> state.
/// </summary>
/// <param name="exceptions">The collection of exceptions to bind to this <see cref="Task"/>. May not be <c>null</c> or contain <c>null</c> elements.</param>
/// <returns><c>true</c> if the operation was successful; otherwise, <c>false</c>.</returns>