using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ZeroLevel.Services.Async
{
///
/// Represents the producer side of a unbound to a delegate, providing access to the consumer side through the property.
///
public sealed class TaskCompletionSource
{
///
/// The underlying TCS.
///
private readonly TaskCompletionSource _tcs;
///
/// Initializes a new instance of the class.
///
public TaskCompletionSource()
{
_tcs = new TaskCompletionSource();
}
///
/// Initializes a new instance of the class with the specified state.
///
/// The state to use as the underlying 's .
public TaskCompletionSource(object state)
{
_tcs = new TaskCompletionSource(state);
}
///
/// Initializes a new instance of the class with the specified options.
///
/// The options to use when creating the underlying .
public TaskCompletionSource(TaskCreationOptions creationOptions)
{
_tcs = new TaskCompletionSource(creationOptions);
}
///
/// Initializes a new instance of the class with the specified state and options.
///
/// The state to use as the underlying 's .
/// The options to use when creating the underlying .
public TaskCompletionSource(object state, TaskCreationOptions creationOptions)
{
_tcs = new TaskCompletionSource(state, creationOptions);
}
///
/// Gets the created by this .
///
public Task Task
{
get { return _tcs.Task; }
}
///
/// Transitions the underlying into the state.
///
/// The underlying has already been completed.
public void SetCanceled()
{
_tcs.SetCanceled();
}
///
/// Attempts to transition the underlying into the state.
///
/// true if the operation was successful; otherwise, false .
public bool TrySetCanceled()
{
return _tcs.TrySetCanceled();
}
///
/// Transitions the underlying into the state.
///
/// The exception to bind to this . May not be null .
/// The underlying has already been completed.
public void SetException(Exception exception)
{
_tcs.SetException(exception);
}
///
/// Transitions the underlying into the state.
///
/// The collection of exceptions to bind to this . May not be null or contain null elements.
/// The underlying has already been completed.
public void SetException(IEnumerable exceptions)
{
_tcs.SetException(exceptions);
}
///
/// Attempts to transition the underlying into the state.
///
/// The exception to bind to this . May not be null .
/// true if the operation was successful; otherwise, false .
public bool TrySetException(Exception exception)
{
return _tcs.TrySetException(exception);
}
///
/// Attempts to transition the underlying into the state.
///
/// The collection of exceptions to bind to this . May not be null or contain null elements.
/// true if the operation was successful; otherwise, false .
public bool TrySetException(IEnumerable exceptions)
{
return _tcs.TrySetException(exceptions);
}
///
/// Transitions the underlying into the state.
///
/// The underlying has already been completed.
public void SetResult()
{
_tcs.SetResult(null);
}
///
/// Attempts to transition the underlying into the state.
///
/// true if the operation was successful; otherwise, false .
public bool TrySetResult()
{
return _tcs.TrySetResult(null);
}
}
}