#if NET20 || NET30 || NET35 || !NET_4_6
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace System.Threading.Tasks
{
///
/// Provides methods for creating and manipulating tasks.
///
///
///
/// TaskEx is a placeholder.
///
public static class TaskEx
{
///
/// Starts a Task that will complete after the specified due time.
///
/// The delay in milliseconds before the returned task completes.
///
/// The timed Task.
///
/// The argument must be non-negative or -1 and less than or equal to Int32.MaxValue.
///
public static Task Delay(int dueTime)
{
return Delay(dueTime, CancellationToken.None);
}
///
/// Starts a Task that will complete after the specified due time.
///
/// The delay before the returned task completes.
///
/// The timed Task.
///
/// The argument must be non-negative or -1 and less than or equal to Int32.MaxValue.
///
public static Task Delay(TimeSpan dueTime)
{
return Delay(dueTime, CancellationToken.None);
}
///
/// Starts a Task that will complete after the specified due time.
///
/// The delay before the returned task completes.A CancellationToken that may be used to cancel the task before the due time occurs.
///
/// The timed Task.
///
/// The argument must be non-negative or -1 and less than or equal to Int32.MaxValue.
///
public static Task Delay(TimeSpan dueTime, CancellationToken cancellationToken)
{
return Task.Delay(dueTime, cancellationToken);
}
///
/// Starts a Task that will complete after the specified due time.
///
/// The delay in milliseconds before the returned task completes.A CancellationToken that may be used to cancel the task before the due time occurs.
///
/// The timed Task.
///
/// The argument must be non-negative or -1 and less than or equal to Int32.MaxValue.
///
public static Task Delay(int dueTime, CancellationToken cancellationToken)
{
return Task.Delay(dueTime, cancellationToken);
}
///
/// Creates an already completed from the specified result.
///
/// The result from which to create the completed task.
///
/// The completed task.
///
public static Task FromResult(TResult result)
{
return Task.FromResult(result);
}
///
/// Creates a task that runs the specified action.
///
/// The action to execute asynchronously.
///
/// A task that represents the completion of the action.
///
/// The argument is null.
public static Task Run(Action action)
{
return Run(action, CancellationToken.None);
}
///
/// Creates a task that runs the specified action.
///
/// The action to execute.The CancellationToken to use to request cancellation of this task.
///
/// A task that represents the completion of the action.
///
/// The argument is null.
public static Task Run(Action action, CancellationToken cancellationToken)
{
return Task.Factory.StartNew(action, cancellationToken, TaskCreationOptions.None, TaskScheduler.Default);
}
///
/// Creates a task that runs the specified function.
///
/// The function to execute asynchronously.
///
/// A task that represents the completion of the action.
///
/// The argument is null.
public static Task Run(Func function)
{
return Run(function, CancellationToken.None);
}
///
/// Creates a task that runs the specified function.
///
/// The action to execute.The CancellationToken to use to cancel the task.
///
/// A task that represents the completion of the action.
///
/// The argument is null.
public static Task Run(Func function, CancellationToken cancellationToken)
{
return Task.Factory.StartNew(function, cancellationToken, TaskCreationOptions.None, TaskScheduler.Default);
}
///
/// Creates a task that runs the specified function.
///
/// The action to execute asynchronously.
///
/// A task that represents the completion of the action.
///
/// The argument is null.
public static Task Run(Func function)
{
return Run(function, CancellationToken.None);
}
///
/// Creates a task that runs the specified function.
///
/// The function to execute.The CancellationToken to use to request cancellation of this task.
///
/// A task that represents the completion of the function.
///
/// The argument is null.
public static Task Run(Func function, CancellationToken cancellationToken)
{
return Run(function, cancellationToken).Unwrap();
}
///
/// Creates a task that runs the specified function.
///
/// The function to execute asynchronously.
///
/// A task that represents the completion of the action.
///
/// The argument is null.
public static Task Run(Func> function)
{
return Run(function, CancellationToken.None);
}
///
/// Creates a task that runs the specified function.
///
/// The action to execute.The CancellationToken to use to cancel the task.
///
/// A task that represents the completion of the action.
///
/// The argument is null.
public static Task Run(Func> function, CancellationToken cancellationToken)
{
return Run>(function, cancellationToken).Unwrap();
}
///
/// Creates a Task that will complete only when all of the provided collection of Tasks has completed.
///
/// The Tasks to monitor for completion.
///
/// A Task that represents the completion of all of the provided tasks.
///
///
///
/// If any of the provided Tasks faults, the returned Task will also fault, and its Exception will contain information
/// about all of the faulted tasks. If no Tasks fault but one or more Tasks is canceled, the returned
/// Task will also be canceled.
///
///
/// The argument is null.The argument contains a null reference.
public static Task WhenAll(params Task[] tasks)
{
return WhenAll((IEnumerable)tasks);
}
///
/// Creates a Task that will complete only when all of the provided collection of Tasks has completed.
///
/// The Tasks to monitor for completion.
///
/// A Task that represents the completion of all of the provided tasks.
///
///
///
/// If any of the provided Tasks faults, the returned Task will also fault, and its Exception will contain information
/// about all of the faulted tasks. If no Tasks fault but one or more Tasks is canceled, the returned
/// Task will also be canceled.
///
///
/// The argument is null.The argument contains a null reference.
public static Task WhenAll(params Task[] tasks)
{
return WhenAll((IEnumerable>)tasks);
}
///
/// Creates a Task that will complete only when all of the provided collection of Tasks has completed.
///
/// The Tasks to monitor for completion.
///
/// A Task that represents the completion of all of the provided tasks.
///
///
///
/// If any of the provided Tasks faults, the returned Task will also fault, and its Exception will contain information
/// about all of the faulted tasks. If no Tasks fault but one or more Tasks is canceled, the returned
/// Task will also be canceled.
///
///
/// The argument is null.The argument contains a null reference.
public static Task WhenAll(IEnumerable tasks)
{
return Task.WhenAll(tasks);
}
///
/// Creates a Task that will complete only when all of the provided collection of Tasks has completed.
///
/// The Tasks to monitor for completion.
///
/// A Task that represents the completion of all of the provided tasks.
///
///
///
/// If any of the provided Tasks faults, the returned Task will also fault, and its Exception will contain information
/// about all of the faulted tasks. If no Tasks fault but one or more Tasks is canceled, the returned
/// Task will also be canceled.
///
///
/// The argument is null.The argument contains a null reference.
public static Task WhenAll(IEnumerable> tasks)
{
return Task.WhenAll(tasks);
}
///
/// Creates a Task that will complete when any of the tasks in the provided collection completes.
///
/// The Tasks to be monitored.
///
/// A Task that represents the completion of any of the provided Tasks. The completed Task is this Task's result.
///
///
///
///
/// Any Tasks that fault will need to have their exceptions observed elsewhere.
///
/// The argument is null.The argument contains a null reference.
public static Task WhenAny(params Task[] tasks)
{
return WhenAny((IEnumerable)tasks);
}
///
/// Creates a Task that will complete when any of the tasks in the provided collection completes.
///
/// The Tasks to be monitored.
///
/// A Task that represents the completion of any of the provided Tasks. The completed Task is this Task's result.
///
///
///
///
/// Any Tasks that fault will need to have their exceptions observed elsewhere.
///
/// The argument is null.The argument contains a null reference.
public static Task WhenAny(IEnumerable tasks)
{
return Task.WhenAny(tasks);
}
///
/// Creates a Task that will complete when any of the tasks in the provided collection completes.
///
/// The Tasks to be monitored.
///
/// A Task that represents the completion of any of the provided Tasks. The completed Task is this Task's result.
///
///
///
///
/// Any Tasks that fault will need to have their exceptions observed elsewhere.
///
/// The argument is null.The argument contains a null reference.
public static Task> WhenAny(params Task[] tasks)
{
return WhenAny((IEnumerable>)tasks);
}
///
/// Creates a Task that will complete when any of the tasks in the provided collection completes.
///
/// The Tasks to be monitored.
///
/// A Task that represents the completion of any of the provided Tasks. The completed Task is this Task's result.
///
///
///
///
/// Any Tasks that fault will need to have their exceptions observed elsewhere.
///
/// The argument is null.The argument contains a null reference.
public static Task> WhenAny(IEnumerable> tasks)
{
return Task.WhenAny(tasks);
}
///
/// Creates an awaitable that asynchronously yields back to the current context when awaited.
///
///
///
/// A context that, when awaited, will asynchronously transition back into the current context.
/// If SynchronizationContext.Current is non-null, that is treated as the current context.
/// Otherwise, TaskScheduler.Current is treated as the current context.
///
///
public static YieldAwaitable Yield()
{
return new YieldAwaitable();
}
}
}
#endif