You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Async/TaskConstants.cs

144 lines
3.5 KiB

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

Powered by TurnKey Linux.