using System.Threading.Tasks;
namespace ZeroLevel.Services.Async
{
///
/// Provides completed task constants.
///
public static class TaskConstants
{
private static readonly Task booleanTrue = TaskShim.FromResult(true);
private static readonly Task intNegativeOne = TaskShim.FromResult(-1);
///
/// A task that has been completed with the value true.
///
public static Task BooleanTrue
{
get
{
return booleanTrue;
}
}
///
/// A task that has been completed with the value false.
///
public static Task BooleanFalse
{
get
{
return TaskConstants.Default;
}
}
///
/// A task that has been completed with the value 0.
///
public static Task Int32Zero
{
get
{
return TaskConstants.Default;
}
}
///
/// A task that has been completed with the value -1.
///
public static Task Int32NegativeOne
{
get
{
return intNegativeOne;
}
}
///
/// A that has been completed.
///
public static Task Completed
{
get
{
return booleanTrue;
}
}
///
/// A that will never complete.
///
public static Task Never
{
get
{
return TaskConstants.Never;
}
}
///
/// A task that has been canceled.
///
public static Task Canceled
{
get
{
return TaskConstants.Canceled;
}
}
}
///
/// Provides completed task constants.
///
/// The type of the task result.
public static class TaskConstants
{
private static readonly Task defaultValue = TaskShim.FromResult(default(T));
private static readonly Task never = new TaskCompletionSource().Task;
private static readonly Task canceled = CanceledTask();
private static Task CanceledTask()
{
var tcs = new TaskCompletionSource();
tcs.SetCanceled();
return tcs.Task;
}
///
/// A task that has been completed with the default value of .
///
public static Task Default
{
get
{
return defaultValue;
}
}
///
/// A that will never complete.
///
public static Task Never
{
get
{
return never;
}
}
///
/// A task that has been canceled.
///
public static Task Canceled
{
get
{
return canceled;
}
}
}
}