Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Threading / Tasks / FutureFactory.cs / 1305376 / FutureFactory.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// FutureFactory.cs
//
// [....]
//
// As with TaskFactory, TaskFactory encodes common factory patterns into helper methods.
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System;
using System.Security;
using System.Security.Permissions;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Diagnostics.Contracts;
namespace System.Threading.Tasks
{
///
/// Provides support for creating and scheduling
/// Task{TResult} objects.
///
/// The type of the results that are available though
/// the Task{TResult} objects that are associated with
/// the methods in this class.
///
///
/// There are many common patterns for which tasks are relevant. The
/// class encodes some of these patterns into methods that pick up default settings, which are
/// configurable through its constructors.
///
///
/// A default instance of is available through the
/// Task{TResult}.Factory property.
///
///
[HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
public class TaskFactory
{
// Member variables, DefaultScheduler, other properties and ctors
// copied right out of TaskFactory... Lots of duplication here...
// Should we be thinking about a TaskFactoryBase class?
// member variables
private CancellationToken m_defaultCancellationToken;
private TaskScheduler m_defaultScheduler;
private TaskCreationOptions m_defaultCreationOptions;
private TaskContinuationOptions m_defaultContinuationOptions;
private TaskScheduler DefaultScheduler
{
get
{
if (m_defaultScheduler == null) return TaskScheduler.Current;
else return m_defaultScheduler;
}
}
// sister method to above property -- avoids a TLS lookup
private TaskScheduler GetDefaultScheduler(Task currTask)
{
if (m_defaultScheduler != null) return m_defaultScheduler;
else if (currTask != null) return currTask.ExecutingTaskScheduler;
else return TaskScheduler.Default;
}
/* Constructors */
///
/// Initializes a instance with the default configuration.
///
///
/// This constructor creates a instance with a default configuration. The
/// property is initialized to
/// TaskCreationOptions.None , the
/// property is initialized to TaskContinuationOptions.None ,
/// and the TaskScheduler property is
/// initialized to the current scheduler (see TaskScheduler.Current ).
///
public TaskFactory()
: this(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, null)
{
}
///
/// Initializes a instance with the default configuration.
///
/// The default that will be assigned
/// to tasks created by this unless another CancellationToken is explicitly specified
/// while calling the factory methods.
///
/// This constructor creates a instance with a default configuration. The
/// property is initialized to
/// TaskCreationOptions.None , the
/// property is initialized to TaskContinuationOptions.None ,
/// and the TaskScheduler property is
/// initialized to the current scheduler (see TaskScheduler.Current ).
///
public TaskFactory(CancellationToken cancellationToken)
: this(cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, null)
{
}
///
/// Initializes a instance with the specified configuration.
///
///
/// The
/// TaskScheduler to use to schedule any tasks created with this TaskFactory{TResult}. A null value
/// indicates that the current TaskScheduler should be used.
///
///
/// With this constructor, the
/// property is initialized to
/// TaskCreationOptions.None , the
/// property is initialized to TaskContinuationOptions.None ,
/// and the TaskScheduler property is
/// initialized to , unless it's null, in which case the property is
/// initialized to the current scheduler (see TaskScheduler.Current ).
///
public TaskFactory(TaskScheduler scheduler) // null means to use TaskScheduler.Current
: this(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
{
}
///
/// Initializes a instance with the specified configuration.
///
///
/// The default
/// TaskCreationOptions to use when creating tasks with this TaskFactory{TResult}.
///
///
/// The default
/// TaskContinuationOptions to use when creating continuation tasks with this TaskFactory{TResult}.
///
///
/// The exception that is thrown when the
/// argument or the
/// argument specifies an invalid value.
///
///
/// With this constructor, the
/// property is initialized to ,
/// the
/// property is initialized to , and the TaskScheduler property is initialized to the
/// current scheduler (see TaskScheduler.Current ).
///
public TaskFactory(TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
: this(CancellationToken.None, creationOptions, continuationOptions, null)
{
}
///
/// Initializes a instance with the specified configuration.
///
/// The default that will be assigned
/// to tasks created by this unless another CancellationToken is explicitly specified
/// while calling the factory methods.
///
/// The default
/// TaskCreationOptions to use when creating tasks with this TaskFactory{TResult}.
///
///
/// The default
/// TaskContinuationOptions to use when creating continuation tasks with this TaskFactory{TResult}.
///
///
/// The default
/// TaskScheduler to use to schedule any Tasks created with this TaskFactory{TResult}. A null value
/// indicates that TaskScheduler.Current should be used.
///
///
/// The exception that is thrown when the
/// argument or the
/// argumentspecifies an invalid value.
///
///
/// With this constructor, the
/// property is initialized to ,
/// the
/// property is initialized to , and the TaskScheduler property is initialized to
/// , unless it's null, in which case the property is initialized to the
/// current scheduler (see TaskScheduler.Current ).
///
public TaskFactory(CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
{
m_defaultCancellationToken = cancellationToken;
m_defaultScheduler = scheduler;
m_defaultCreationOptions = creationOptions;
m_defaultContinuationOptions = continuationOptions;
TaskFactory.CheckCreationOptions(m_defaultCreationOptions);
TaskFactory.CheckMultiTaskContinuationOptions(m_defaultContinuationOptions);
}
/* Properties */
///
/// Gets the default CancellationToken of this
/// TaskFactory.
///
///
/// This property returns the default that will be assigned to all
/// tasks created by this factory unless another CancellationToken value is explicitly specified
/// during the call to the factory methods.
///
public CancellationToken CancellationToken { get { return m_defaultCancellationToken; } }
///
/// Gets the TaskScheduler of this
/// TaskFactory{TResult}.
///
///
/// This property returns the default scheduler for this factory. It will be used to schedule all
/// tasks unless another scheduler is explicitly specified during calls to this factory's methods.
/// If null, TaskScheduler.Current
/// will be used.
///
public TaskScheduler Scheduler { get { return m_defaultScheduler; } }
///
/// Gets the TaskCreationOptions
/// value of this TaskFactory{TResult}.
///
///
/// This property returns the default creation options for this factory. They will be used to create all
/// tasks unless other options are explicitly specified during calls to this factory's methods.
///
public TaskCreationOptions CreationOptions { get { return m_defaultCreationOptions; } }
///
/// Gets the TaskContinuationOptions
/// value of this TaskFactory{TResult}.
///
///
/// This property returns the default continuation options for this factory. They will be used to create
/// all continuation tasks unless other options are explicitly specified during calls to this factory's methods.
///
public TaskContinuationOptions ContinuationOptions { get { return m_defaultContinuationOptions; } }
/* StartNew */
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// The started .
/// The exception that is thrown when the
/// argument is null.
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, m_defaultCancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// The that will be assigned to the new task.
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, CancellationToken cancellationToken)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, cancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// A TaskCreationOptions value that controls the behavior of the
/// created
/// .
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, TaskCreationOptions creationOptions)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, m_defaultCancellationToken,
creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// A TaskCreationOptions value that controls the behavior of the
/// created
/// .
/// The that will be assigned to the new task.
/// The TaskScheduler
/// that is used to schedule the created
/// Task{TResult} .
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return Task.StartNew(Task.InternalCurrent, function, cancellationToken,
creationOptions, InternalTaskOptions.None, scheduler, ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// An object containing data to be used by the
/// delegate.
/// The started .
/// The exception that is thrown when the
/// argument is null.
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, Object state)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, state, m_defaultCancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// An object containing data to be used by the
/// delegate.
/// The that will be assigned to the new task.
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, Object state, CancellationToken cancellationToken)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, state, cancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// An object containing data to be used by the
/// delegate.
/// A TaskCreationOptions value that controls the behavior of the
/// created
/// .
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, Object state, TaskCreationOptions creationOptions)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, state, m_defaultCancellationToken,
creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// An object containing data to be used by the
/// delegate.
/// The that will be assigned to the new task.
/// A TaskCreationOptions value that controls the behavior of the
/// created
/// .
/// The TaskScheduler
/// that is used to schedule the created
/// Task{TResult} .
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, Object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return Task.StartNew(Task.InternalCurrent, function, state, cancellationToken,
creationOptions, InternalTaskOptions.None, scheduler, ref stackMark);
}
//
// APM Factory methods
//
// Common core logic for FromAsync calls. This minimizes the chance of "drift" between overload implementations.
private static void FromAsyncCoreLogic(IAsyncResult iar, Func endMethod, TaskCompletionSource tcs)
{
Exception ex = null;
OperationCanceledException oce = null;
TResult result = default(TResult);
try { result = endMethod(iar); }
catch (OperationCanceledException _oce) { oce = _oce; }
catch (Exception e) { ex = e; }
finally
{
if (oce != null) tcs.TrySetCanceled();
else if (ex != null)
{
bool bWonSetException = tcs.TrySetException(ex);
if (bWonSetException && ex is ThreadAbortException)
{
tcs.Task.m_contingentProperties.m_exceptionsHolder.MarkAsHandled(false);
}
}
else tcs.TrySetResult(result);
}
}
///
/// Creates a Task that executes an end
/// method function when a specified IAsyncResult completes.
///
/// The IAsyncResult whose completion should trigger the processing of the
/// .
/// The function delegate that processes the completed .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// A Task that represents the
/// asynchronous operation.
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task FromAsync(
IAsyncResult asyncResult,
Func endMethod)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return FromAsyncImpl(asyncResult, endMethod, m_defaultCreationOptions, DefaultScheduler, ref stackMark);
}
///
/// Creates a Task that executes an end
/// method function when a specified IAsyncResult completes.
///
/// The IAsyncResult whose completion should trigger the processing of the
/// .
/// The function delegate that processes the completed .
/// The TaskCreationOptions value that controls the behavior of the
/// created Task .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// A Task that represents the
/// asynchronous operation.
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task FromAsync(
IAsyncResult asyncResult,
Func endMethod,
TaskCreationOptions creationOptions)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return FromAsyncImpl(asyncResult, endMethod, creationOptions, DefaultScheduler, ref stackMark);
}
///
/// Creates a Task that executes an end
/// method function when a specified IAsyncResult completes.
///
/// The IAsyncResult whose completion should trigger the processing of the
/// .
/// The function delegate that processes the completed .
/// The TaskScheduler
/// that is used to schedule the task that executes the end method.
/// The TaskCreationOptions value that controls the behavior of the
/// created Task .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// A Task that represents the
/// asynchronous operation.
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task FromAsync(
IAsyncResult asyncResult,
Func endMethod,
TaskCreationOptions creationOptions,
TaskScheduler scheduler)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return FromAsyncImpl(asyncResult, endMethod, creationOptions, scheduler, ref stackMark);
}
// internal overload that supports StackCrawlMark
// We also need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory instance.
internal static Task FromAsyncImpl(
IAsyncResult asyncResult,
Func endMethod,
TaskCreationOptions creationOptions,
TaskScheduler scheduler,
ref StackCrawlMark stackMark)
{
if (asyncResult == null)
throw new ArgumentNullException("asyncResult");
if (endMethod == null)
throw new ArgumentNullException("endMethod");
if (scheduler == null)
throw new ArgumentNullException("scheduler");
TaskFactory.CheckFromAsyncOptions(creationOptions, false);
TaskCompletionSource tcs = new TaskCompletionSource(creationOptions);
// Just specify this task as detached. No matter what happens, we want endMethod
// to be called -- even if the parent is canceled. So we don't want to flow
// RespectParentCancellation.
Task t = new Task(delegate
{
FromAsyncCoreLogic(asyncResult, endMethod, tcs);
},
(object)null, Task.InternalCurrent,
CancellationToken.None, TaskCreationOptions.None, InternalTaskOptions.None, null, ref stackMark);
if (asyncResult.IsCompleted)
{
try { t.RunSynchronously(scheduler); }
catch (Exception e) { tcs.TrySetException(e); } // catch and log any scheduler exceptions
}
else
{
ThreadPool.RegisterWaitForSingleObject(
asyncResult.AsyncWaitHandle,
delegate
{
try { t.RunSynchronously(scheduler); }
catch (Exception e) { tcs.TrySetException(e); } // catch and log any scheduler exceptions
},
null,
Timeout.Infinite,
true);
}
return tcs.Task;
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod, object state)
{
return FromAsyncImpl(beginMethod, endMethod, state, m_defaultCreationOptions);
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// The TaskCreationOptions value that controls the behavior of the
/// created Task .
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod, object state, TaskCreationOptions creationOptions)
{
return FromAsyncImpl(beginMethod, endMethod, state, creationOptions);
}
// We need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory instance.
internal static Task FromAsyncImpl(
Func beginMethod,
Func endMethod, object state, TaskCreationOptions creationOptions)
{
if (beginMethod == null)
throw new ArgumentNullException("beginMethod");
if (endMethod == null)
throw new ArgumentNullException("endMethod");
TaskFactory.CheckFromAsyncOptions(creationOptions, true);
TaskCompletionSource tcs = new TaskCompletionSource(state, creationOptions);
try
{
beginMethod(iar =>
{
FromAsyncCoreLogic(iar, endMethod, tcs);
}, state);
}
catch
{
// Make sure we don't leave tcs "dangling".
tcs.TrySetResult(default(TResult));
throw;
}
return tcs.Task;
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The type of the first argument passed to the delegate.
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// The first argument passed to the
/// delegate.
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod,
TArg1 arg1, object state)
{
return FromAsyncImpl(beginMethod, endMethod, arg1, state, m_defaultCreationOptions);
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The type of the first argument passed to the delegate.
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// The first argument passed to the
/// delegate.
/// The TaskCreationOptions value that controls the behavior of the
/// created Task .
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod,
TArg1 arg1, object state, TaskCreationOptions creationOptions)
{
return FromAsyncImpl(beginMethod, endMethod, arg1, state, creationOptions);
}
// We need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory instance.
internal static Task FromAsyncImpl(
Func beginMethod,
Func endMethod,
TArg1 arg1, object state, TaskCreationOptions creationOptions)
{
if (beginMethod == null)
throw new ArgumentNullException("beginMethod");
if (endMethod == null)
throw new ArgumentNullException("endMethod");
TaskFactory.CheckFromAsyncOptions(creationOptions, true);
TaskCompletionSource tcs = new TaskCompletionSource(state, creationOptions);
try
{
beginMethod(arg1, iar =>
{
FromAsyncCoreLogic(iar, endMethod, tcs);
}, state);
}
catch
{
// Make sure we don't leave tcs "dangling".
tcs.TrySetResult(default(TResult));
throw;
}
return tcs.Task;
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The type of the first argument passed to the delegate.
/// The type of the second argument passed to
/// delegate.
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// The first argument passed to the
/// delegate.
/// The second argument passed to the
/// delegate.
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod,
TArg1 arg1, TArg2 arg2, object state)
{
return FromAsyncImpl(beginMethod, endMethod, arg1, arg2, state, m_defaultCreationOptions);
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The type of the first argument passed to the delegate.
/// The type of the second argument passed to
/// delegate.
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// The first argument passed to the
/// delegate.
/// The second argument passed to the
/// delegate.
/// The TaskCreationOptions value that controls the behavior of the
/// created Task .
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod,
TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
{
return FromAsyncImpl(beginMethod, endMethod, arg1, arg2, state, creationOptions);
}
// We need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory instance.
internal static Task FromAsyncImpl(
Func beginMethod,
Func endMethod,
TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
{
if (beginMethod == null)
throw new ArgumentNullException("beginMethod");
if (endMethod == null)
throw new ArgumentNullException("endMethod");
TaskFactory.CheckFromAsyncOptions(creationOptions, true);
TaskCompletionSource tcs = new TaskCompletionSource(state, creationOptions);
try
{
beginMethod(arg1, arg2, iar =>
{
FromAsyncCoreLogic(iar, endMethod, tcs);
}, state);
}
catch
{
// Make sure we don't leave tcs "dangling".
tcs.TrySetResult(default(TResult));
throw;
}
return tcs.Task;
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The type of the first argument passed to the delegate.
/// The type of the second argument passed to
/// delegate.
/// The type of the third argument passed to
/// delegate.
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// The first argument passed to the
/// delegate.
/// The second argument passed to the
/// delegate.
/// The third argument passed to the
/// delegate.
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod,
TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
{
return FromAsyncImpl(beginMethod, endMethod, arg1, arg2, arg3, state, m_defaultCreationOptions);
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The type of the first argument passed to the delegate.
/// The type of the second argument passed to
/// delegate.
/// The type of the third argument passed to
/// delegate.
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// The first argument passed to the
/// delegate.
/// The second argument passed to the
/// delegate.
/// The third argument passed to the
/// delegate.
/// The TaskCreationOptions value that controls the behavior of the
/// created Task .
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod,
TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
{
return FromAsyncImpl(beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
}
// We need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory instance.
internal static Task FromAsyncImpl(
Func beginMethod,
Func endMethod,
TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
{
if (beginMethod == null)
throw new ArgumentNullException("beginMethod");
if (endMethod == null)
throw new ArgumentNullException("endMethod");
TaskFactory.CheckFromAsyncOptions(creationOptions, true);
TaskCompletionSource tcs = new TaskCompletionSource(state, creationOptions);
try
{
beginMethod(arg1, arg2, arg3, iar =>
{
FromAsyncCoreLogic(iar, endMethod, tcs);
}, state);
}
catch
{
// Make sure we don't leave tcs "dangling".
tcs.TrySetResult(default(TResult));
throw;
}
return tcs.Task;
}
// Utility method to create a canceled future-style task.
// Used by ContinueWhenAll/Any to bail out early on a pre-canceled token.
private static Task CreateCanceledTask(TaskContinuationOptions continuationOptions)
{
TaskCreationOptions tco;
InternalTaskOptions dontcare;
Task.CreationOptionsFromContinuationOptions(continuationOptions, out tco, out dontcare);
return new Task(true, default(TResult), tco);
}
//
// ContinueWhenAll() methods
//
///
/// Creates a continuation Task
/// that will be started upon the completion of a set of provided Tasks.
///
/// The array of tasks from which to continue.
/// The function delegate to execute when all tasks in
/// the array have completed.
/// The new continuation Task .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAll(Task[] tasks, Func continuationFunction)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAll(tasks, continuationFunction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of a set of provided Tasks.
///
/// The array of tasks from which to continue.
/// The function delegate to execute when all tasks in
/// the array have completed.
/// The CancellationToken
/// that will be assigned to the new continuation task.
/// The new continuation Task .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
/// The provided CancellationToken
/// has already been disposed.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAll(Task[] tasks, Func continuationFunction, CancellationToken cancellationToken)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAll(tasks, continuationFunction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of a set of provided Tasks.
///
/// The array of tasks from which to continue.
/// The function delegate to execute when all tasks in the array have completed.
/// The
/// TaskContinuationOptions value that controls the behavior of
/// the created continuation Task .
/// The new continuation Task .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when the
/// argument specifies an invalid TaskContinuationOptions
/// value.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
///
/// The NotOn* and OnlyOn* TaskContinuationOptions ,
/// which constrain for which TaskStatus states a continuation
/// will be executed, are illegal with ContinueWhenAll.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAll(Task[] tasks, Func continuationFunction, TaskContinuationOptions continuationOptions)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAll(tasks, continuationFunction, continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of a set of provided Tasks.
///
/// The array of tasks from which to continue.
/// The function delegate to execute when all tasks in the array have completed.
/// The CancellationToken
/// that will be assigned to the new continuation task.
/// The
/// TaskContinuationOptions value that controls the behavior of
/// the created continuation Task .
/// The TaskScheduler
/// that is used to schedule the created continuation Task .
/// The new continuation Task .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when the
/// argument specifies an invalid TaskContinuationOptions
/// value.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// The NotOn* and OnlyOn* TaskContinuationOptions ,
/// which constrain for which TaskStatus states a continuation
/// will be executed, are illegal with ContinueWhenAll.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAll(Task[] tasks, Func continuationFunction,
CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAll(tasks, continuationFunction, continuationOptions, cancellationToken, scheduler, ref stackMark);
}
// Private method to support StackCrawlMark.
internal static Task ContinueWhenAll(Task[] tasks, Func continuationFunction,
TaskContinuationOptions continuationOptions, CancellationToken cancellationToken, TaskScheduler scheduler, ref StackCrawlMark stackMark)
{
// check arguments
TaskFactory.CheckMultiTaskContinuationOptions(continuationOptions);
if (tasks == null) throw new ArgumentNullException("tasks");
if (continuationFunction == null) throw new ArgumentNullException("continuationFunction");
if (scheduler == null) throw new ArgumentNullException("scheduler");
// Check tasks array and make defensive copy
Task[] tasksCopy = TaskFactory.CheckMultiContinuationTasksAndCopy(tasks);
// Bail early if cancellation has been requested.
if (cancellationToken.IsCancellationRequested)
{
return CreateCanceledTask(continuationOptions);
}
// Perform common ContinueWhenAll() setup logic, extract starter task
Task starter = TaskFactory.CommonCWAllLogic(tasksCopy);
// returned continuation task, off of starter
Task rval = starter.ContinueWith(finishedTask => { return continuationFunction(tasksCopy); }, scheduler,
cancellationToken, continuationOptions, ref stackMark);
return rval;
}
///
/// Creates a continuation Task
/// that will be started upon the completion of a set of provided Tasks.
///
/// The type of the result of the antecedent .
/// The array of tasks from which to continue.
/// The function delegate to execute when all tasks in the
/// array have completed.
/// The new continuation .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAll(tasks, continuationFunction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of a set of provided Tasks.
///
/// The type of the result of the antecedent .
/// The array of tasks from which to continue.
/// The function delegate to execute when all tasks in the
/// array have completed.
/// The CancellationToken
/// that will be assigned to the new continuation task.
/// The new continuation .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
/// The provided CancellationToken
/// has already been disposed.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction,
CancellationToken cancellationToken)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAll(tasks, continuationFunction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of a set of provided Tasks.
///
/// The type of the result of the antecedent .
/// The array of tasks from which to continue.
/// The function delegate to execute when all tasks in the
/// array have completed.
/// The
/// TaskContinuationOptions value that controls the behavior of
/// the created continuation Task .
/// The new continuation .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when the
/// argument specifies an invalid TaskContinuationOptions
/// value.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
///
/// The NotOn* and OnlyOn* TaskContinuationOptions ,
/// which constrain for which TaskStatus states a continuation
/// will be executed, are illegal with ContinueWhenAll.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction,
TaskContinuationOptions continuationOptions)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAll(tasks, continuationFunction, continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of a set of provided Tasks.
///
/// The type of the result of the antecedent .
/// The array of tasks from which to continue.
/// The function delegate to execute when all tasks in the
/// array have completed.
/// The CancellationToken
/// that will be assigned to the new continuation task.
/// The
/// TaskContinuationOptions value that controls the behavior of
/// the created continuation Task .
/// The TaskScheduler
/// that is used to schedule the created continuation .
/// The new continuation .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when the
/// argument specifies an invalid TaskContinuationOptions
/// value.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// The NotOn* and OnlyOn* TaskContinuationOptions ,
/// which constrain for which TaskStatus states a continuation
/// will be executed, are illegal with ContinueWhenAll.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction,
CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAll(tasks, continuationFunction, continuationOptions, cancellationToken, scheduler, ref stackMark);
}
// internal (instead of private) so that it can be called from the similar TaskFactory.ContinueWhenAll() method.
internal static Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction,
TaskContinuationOptions continuationOptions, CancellationToken cancellationToken, TaskScheduler scheduler, ref StackCrawlMark stackMark)
{
// check arguments
TaskFactory.CheckMultiTaskContinuationOptions(continuationOptions);
if (tasks == null) throw new ArgumentNullException("tasks");
if (continuationFunction == null) throw new ArgumentNullException("continuationFunction");
if (scheduler == null) throw new ArgumentNullException("scheduler");
// Check tasks array and make defensive copy
Task[] tasksCopy = TaskFactory.CheckMultiContinuationTasksAndCopy(tasks);
// Bail early if cancellation has been requested.
if (cancellationToken.IsCancellationRequested)
{
return CreateCanceledTask(continuationOptions);
}
// Call common ContinueWhenAll() setup logic, extract starter task.
Task starter = TaskFactory.CommonCWAllLogic(tasksCopy);
// returned continuation task, off of starter
Task rval = starter.ContinueWith(finishedTask => { return continuationFunction(tasksCopy); }, scheduler,
cancellationToken, continuationOptions, ref stackMark);
return rval;
}
//
// ContinueWhenAny() methods
//
///
/// Creates a continuation Task
/// that will be started upon the completion of any Task in the provided set.
///
/// The array of tasks from which to continue when one task completes.
/// The function delegate to execute when one task in the array completes.
/// The new continuation Task .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAny(Task[] tasks, Func continuationFunction)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAny(tasks, continuationFunction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of any Task in the provided set.
///
/// The array of tasks from which to continue when one task completes.
/// The function delegate to execute when one task in the array completes.
/// The CancellationToken
/// that will be assigned to the new continuation task.
/// The new continuation Task .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
/// The provided CancellationToken
/// has already been disposed.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAny(Task[] tasks, Func continuationFunction, CancellationToken cancellationToken)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAny(tasks, continuationFunction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of any Task in the provided set.
///
/// The array of tasks from which to continue when one task completes.
/// The function delegate to execute when one task in the array completes.
/// The
/// TaskContinuationOptions value that controls the behavior of
/// the created continuation Task .
/// The new continuation Task .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when the
/// argument specifies an invalid TaskContinuationOptions
/// value.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
///
/// The NotOn* and OnlyOn* TaskContinuationOptions ,
/// which constrain for which TaskStatus states a continuation
/// will be executed, are illegal with ContinueWhenAny.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAny(Task[] tasks, Func continuationFunction, TaskContinuationOptions continuationOptions)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAny(tasks, continuationFunction, continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of any Task in the provided set.
///
/// The array of tasks from which to continue when one task completes.
/// The function delegate to execute when one task in the array completes.
/// The CancellationToken
/// that will be assigned to the new continuation task.
/// The
/// TaskContinuationOptions value that controls the behavior of
/// the created continuation Task .
/// The TaskScheduler
/// that is used to schedule the created continuation Task .
/// The new continuation Task .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when the
/// argument specifies an invalid TaskContinuationOptions
/// value.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// The NotOn* and OnlyOn* TaskContinuationOptions ,
/// which constrain for which TaskStatus states a continuation
/// will be executed, are illegal with ContinueWhenAny.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAny(Task[] tasks, Func continuationFunction,
CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAny(tasks, continuationFunction, continuationOptions, cancellationToken, scheduler, ref stackMark);
}
// internal version that supports StackCrawlMark
internal static Task ContinueWhenAny(Task[] tasks, Func continuationFunction,
TaskContinuationOptions continuationOptions, CancellationToken cancellationToken, TaskScheduler scheduler, ref StackCrawlMark stackMark)
{
// check arguments
TaskFactory.CheckMultiTaskContinuationOptions(continuationOptions);
if (tasks == null) throw new ArgumentNullException("tasks");
if (continuationFunction == null) throw new ArgumentNullException("continuationFunction");
if (scheduler == null) throw new ArgumentNullException("scheduler");
// Check tasks array and make defensive copy
Task[] tasksCopy = TaskFactory.CheckMultiContinuationTasksAndCopy(tasks);
// Bail early if cancellation has been requested.
if (cancellationToken.IsCancellationRequested)
{
return CreateCanceledTask(continuationOptions);
}
// Call common ContinueWhenAny() setup logic, extract starter
Task starter = TaskFactory.CommonCWAnyLogic(tasksCopy);
// returned continuation task, off of starter
Task rval = starter.ContinueWith(completedTask =>
{
return continuationFunction(completedTask.Result);
}, scheduler, cancellationToken, continuationOptions, ref stackMark);
return rval;
}
///
/// Creates a continuation Task
/// that will be started upon the completion of any Task in the provided set.
///
/// The type of the result of the antecedent .
/// The array of tasks from which to continue when one task completes.
/// The function delegate to execute when one task in the
/// array completes.
/// The new continuation .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAny(tasks, continuationFunction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of any Task in the provided set.
///
/// The type of the result of the antecedent .
/// The array of tasks from which to continue when one task completes.
/// The function delegate to execute when one task in the
/// array completes.
/// The CancellationToken
/// that will be assigned to the new continuation task.
/// The new continuation .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
/// The provided CancellationToken
/// has already been disposed.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction,
CancellationToken cancellationToken)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAny(tasks, continuationFunction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of any Task in the provided set.
///
/// The type of the result of the antecedent .
/// The array of tasks from which to continue when one task completes.
/// The function delegate to execute when one task in the
/// array completes.
/// The
/// TaskContinuationOptions value that controls the behavior of
/// the created continuation Task .
/// The new continuation .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when the
/// argument specifies an invalid TaskContinuationOptions
/// value.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
///
/// The NotOn* and OnlyOn* TaskContinuationOptions ,
/// which constrain for which TaskStatus states a continuation
/// will be executed, are illegal with ContinueWhenAny.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction,
TaskContinuationOptions continuationOptions)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAny(tasks, continuationFunction, continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark);
}
///
/// Creates a continuation Task
/// that will be started upon the completion of any Task in the provided set.
///
/// The type of the result of the antecedent .
/// The array of tasks from which to continue when one task completes.
/// The function delegate to execute when one task in the
/// array completes.
/// The CancellationToken
/// that will be assigned to the new continuation task.
/// The
/// TaskContinuationOptions value that controls the behavior of
/// the created continuation Task .
/// The TaskScheduler
/// that is used to schedule the created continuation .
/// The new continuation .
/// The exception that is thrown when the
/// array is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// array contains a null value.
/// The exception that is thrown when the
/// array is empty.
/// The exception that is thrown when the
/// argument specifies an invalid TaskContinuationOptions
/// value.
/// The exception that is thrown when one
/// of the elements in the array has been disposed.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// The NotOn* and OnlyOn* TaskContinuationOptions ,
/// which constrain for which TaskStatus states a continuation
/// will be executed, are illegal with ContinueWhenAny.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction,
CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return ContinueWhenAny(tasks, continuationFunction, continuationOptions, cancellationToken, scheduler, ref stackMark);
}
// internal version to support StackCrawlMark
internal static Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction,
TaskContinuationOptions continuationOptions, CancellationToken cancellationToken, TaskScheduler scheduler, ref StackCrawlMark stackMark)
{
// check arguments
TaskFactory.CheckMultiTaskContinuationOptions(continuationOptions);
if (tasks == null) throw new ArgumentNullException("tasks");
if (continuationFunction == null) throw new ArgumentNullException("continuationFunction");
if (scheduler == null) throw new ArgumentNullException("scheduler");
// Check tasks array and make defensive copy
Task[] tasksCopy = TaskFactory.CheckMultiContinuationTasksAndCopy(tasks);
// Bail early if cancellation has been requested.
if (cancellationToken.IsCancellationRequested)
{
return CreateCanceledTask(continuationOptions);
}
// Call common ContinueWhenAny setup logic, extract starter
Task starter = TaskFactory.CommonCWAnyLogic(tasksCopy);
// returned continuation task, off of starter
Task rval = starter.ContinueWith(completedTask =>
{
Task winner = completedTask.Result as Task;
return continuationFunction(winner);
}, scheduler, cancellationToken, continuationOptions, ref stackMark);
return rval;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// FutureFactory.cs
//
// [....]
//
// As with TaskFactory, TaskFactory encodes common factory patterns into helper methods.
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System;
using System.Security;
using System.Security.Permissions;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Diagnostics.Contracts;
namespace System.Threading.Tasks
{
///
/// Provides support for creating and scheduling
/// Task{TResult} objects.
///
/// The type of the results that are available though
/// the Task{TResult} objects that are associated with
/// the methods in this class.
///
///
/// There are many common patterns for which tasks are relevant. The
/// class encodes some of these patterns into methods that pick up default settings, which are
/// configurable through its constructors.
///
///
/// A default instance of is available through the
/// Task{TResult}.Factory property.
///
///
[HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
public class TaskFactory
{
// Member variables, DefaultScheduler, other properties and ctors
// copied right out of TaskFactory... Lots of duplication here...
// Should we be thinking about a TaskFactoryBase class?
// member variables
private CancellationToken m_defaultCancellationToken;
private TaskScheduler m_defaultScheduler;
private TaskCreationOptions m_defaultCreationOptions;
private TaskContinuationOptions m_defaultContinuationOptions;
private TaskScheduler DefaultScheduler
{
get
{
if (m_defaultScheduler == null) return TaskScheduler.Current;
else return m_defaultScheduler;
}
}
// sister method to above property -- avoids a TLS lookup
private TaskScheduler GetDefaultScheduler(Task currTask)
{
if (m_defaultScheduler != null) return m_defaultScheduler;
else if (currTask != null) return currTask.ExecutingTaskScheduler;
else return TaskScheduler.Default;
}
/* Constructors */
///
/// Initializes a instance with the default configuration.
///
///
/// This constructor creates a instance with a default configuration. The
/// property is initialized to
/// TaskCreationOptions.None , the
/// property is initialized to TaskContinuationOptions.None ,
/// and the TaskScheduler property is
/// initialized to the current scheduler (see TaskScheduler.Current ).
///
public TaskFactory()
: this(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, null)
{
}
///
/// Initializes a instance with the default configuration.
///
/// The default that will be assigned
/// to tasks created by this unless another CancellationToken is explicitly specified
/// while calling the factory methods.
///
/// This constructor creates a instance with a default configuration. The
/// property is initialized to
/// TaskCreationOptions.None , the
/// property is initialized to TaskContinuationOptions.None ,
/// and the TaskScheduler property is
/// initialized to the current scheduler (see TaskScheduler.Current ).
///
public TaskFactory(CancellationToken cancellationToken)
: this(cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, null)
{
}
///
/// Initializes a instance with the specified configuration.
///
///
/// The
/// TaskScheduler to use to schedule any tasks created with this TaskFactory{TResult}. A null value
/// indicates that the current TaskScheduler should be used.
///
///
/// With this constructor, the
/// property is initialized to
/// TaskCreationOptions.None , the
/// property is initialized to TaskContinuationOptions.None ,
/// and the TaskScheduler property is
/// initialized to , unless it's null, in which case the property is
/// initialized to the current scheduler (see TaskScheduler.Current ).
///
public TaskFactory(TaskScheduler scheduler) // null means to use TaskScheduler.Current
: this(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
{
}
///
/// Initializes a instance with the specified configuration.
///
///
/// The default
/// TaskCreationOptions to use when creating tasks with this TaskFactory{TResult}.
///
///
/// The default
/// TaskContinuationOptions to use when creating continuation tasks with this TaskFactory{TResult}.
///
///
/// The exception that is thrown when the
/// argument or the
/// argument specifies an invalid value.
///
///
/// With this constructor, the
/// property is initialized to ,
/// the
/// property is initialized to , and the TaskScheduler property is initialized to the
/// current scheduler (see TaskScheduler.Current ).
///
public TaskFactory(TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
: this(CancellationToken.None, creationOptions, continuationOptions, null)
{
}
///
/// Initializes a instance with the specified configuration.
///
/// The default that will be assigned
/// to tasks created by this unless another CancellationToken is explicitly specified
/// while calling the factory methods.
///
/// The default
/// TaskCreationOptions to use when creating tasks with this TaskFactory{TResult}.
///
///
/// The default
/// TaskContinuationOptions to use when creating continuation tasks with this TaskFactory{TResult}.
///
///
/// The default
/// TaskScheduler to use to schedule any Tasks created with this TaskFactory{TResult}. A null value
/// indicates that TaskScheduler.Current should be used.
///
///
/// The exception that is thrown when the
/// argument or the
/// argumentspecifies an invalid value.
///
///
/// With this constructor, the
/// property is initialized to ,
/// the
/// property is initialized to , and the TaskScheduler property is initialized to
/// , unless it's null, in which case the property is initialized to the
/// current scheduler (see TaskScheduler.Current ).
///
public TaskFactory(CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
{
m_defaultCancellationToken = cancellationToken;
m_defaultScheduler = scheduler;
m_defaultCreationOptions = creationOptions;
m_defaultContinuationOptions = continuationOptions;
TaskFactory.CheckCreationOptions(m_defaultCreationOptions);
TaskFactory.CheckMultiTaskContinuationOptions(m_defaultContinuationOptions);
}
/* Properties */
///
/// Gets the default CancellationToken of this
/// TaskFactory.
///
///
/// This property returns the default that will be assigned to all
/// tasks created by this factory unless another CancellationToken value is explicitly specified
/// during the call to the factory methods.
///
public CancellationToken CancellationToken { get { return m_defaultCancellationToken; } }
///
/// Gets the TaskScheduler of this
/// TaskFactory{TResult}.
///
///
/// This property returns the default scheduler for this factory. It will be used to schedule all
/// tasks unless another scheduler is explicitly specified during calls to this factory's methods.
/// If null, TaskScheduler.Current
/// will be used.
///
public TaskScheduler Scheduler { get { return m_defaultScheduler; } }
///
/// Gets the TaskCreationOptions
/// value of this TaskFactory{TResult}.
///
///
/// This property returns the default creation options for this factory. They will be used to create all
/// tasks unless other options are explicitly specified during calls to this factory's methods.
///
public TaskCreationOptions CreationOptions { get { return m_defaultCreationOptions; } }
///
/// Gets the TaskContinuationOptions
/// value of this TaskFactory{TResult}.
///
///
/// This property returns the default continuation options for this factory. They will be used to create
/// all continuation tasks unless other options are explicitly specified during calls to this factory's methods.
///
public TaskContinuationOptions ContinuationOptions { get { return m_defaultContinuationOptions; } }
/* StartNew */
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// The started .
/// The exception that is thrown when the
/// argument is null.
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, m_defaultCancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// The that will be assigned to the new task.
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, CancellationToken cancellationToken)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, cancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// A TaskCreationOptions value that controls the behavior of the
/// created
/// .
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, TaskCreationOptions creationOptions)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, m_defaultCancellationToken,
creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// A TaskCreationOptions value that controls the behavior of the
/// created
/// .
/// The that will be assigned to the new task.
/// The TaskScheduler
/// that is used to schedule the created
/// Task{TResult} .
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return Task.StartNew(Task.InternalCurrent, function, cancellationToken,
creationOptions, InternalTaskOptions.None, scheduler, ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// An object containing data to be used by the
/// delegate.
/// The started .
/// The exception that is thrown when the
/// argument is null.
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, Object state)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, state, m_defaultCancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// An object containing data to be used by the
/// delegate.
/// The that will be assigned to the new task.
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, Object state, CancellationToken cancellationToken)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, state, cancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// An object containing data to be used by the
/// delegate.
/// A TaskCreationOptions value that controls the behavior of the
/// created
/// .
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, Object state, TaskCreationOptions creationOptions)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
Task currTask = Task.InternalCurrent;
return Task.StartNew(currTask, function, state, m_defaultCancellationToken,
creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark);
}
///
/// Creates and starts a .
///
/// A function delegate that returns the future result to be available through
/// the .
/// An object containing data to be used by the
/// delegate.
/// The that will be assigned to the new task.
/// A TaskCreationOptions value that controls the behavior of the
/// created
/// .
/// The TaskScheduler
/// that is used to schedule the created
/// Task{TResult} .
/// The started .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// The provided CancellationToken
/// has already been disposed.
///
///
/// Calling StartNew is functionally equivalent to creating a using one
/// of its constructors and then calling
/// Start to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
///
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task StartNew(Func function, Object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return Task.StartNew(Task.InternalCurrent, function, state, cancellationToken,
creationOptions, InternalTaskOptions.None, scheduler, ref stackMark);
}
//
// APM Factory methods
//
// Common core logic for FromAsync calls. This minimizes the chance of "drift" between overload implementations.
private static void FromAsyncCoreLogic(IAsyncResult iar, Func endMethod, TaskCompletionSource tcs)
{
Exception ex = null;
OperationCanceledException oce = null;
TResult result = default(TResult);
try { result = endMethod(iar); }
catch (OperationCanceledException _oce) { oce = _oce; }
catch (Exception e) { ex = e; }
finally
{
if (oce != null) tcs.TrySetCanceled();
else if (ex != null)
{
bool bWonSetException = tcs.TrySetException(ex);
if (bWonSetException && ex is ThreadAbortException)
{
tcs.Task.m_contingentProperties.m_exceptionsHolder.MarkAsHandled(false);
}
}
else tcs.TrySetResult(result);
}
}
///
/// Creates a Task that executes an end
/// method function when a specified IAsyncResult completes.
///
/// The IAsyncResult whose completion should trigger the processing of the
/// .
/// The function delegate that processes the completed .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// A Task that represents the
/// asynchronous operation.
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task FromAsync(
IAsyncResult asyncResult,
Func endMethod)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return FromAsyncImpl(asyncResult, endMethod, m_defaultCreationOptions, DefaultScheduler, ref stackMark);
}
///
/// Creates a Task that executes an end
/// method function when a specified IAsyncResult completes.
///
/// The IAsyncResult whose completion should trigger the processing of the
/// .
/// The function delegate that processes the completed .
/// The TaskCreationOptions value that controls the behavior of the
/// created Task .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// A Task that represents the
/// asynchronous operation.
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task FromAsync(
IAsyncResult asyncResult,
Func endMethod,
TaskCreationOptions creationOptions)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return FromAsyncImpl(asyncResult, endMethod, creationOptions, DefaultScheduler, ref stackMark);
}
///
/// Creates a Task that executes an end
/// method function when a specified IAsyncResult completes.
///
/// The IAsyncResult whose completion should trigger the processing of the
/// .
/// The function delegate that processes the completed .
/// The TaskScheduler
/// that is used to schedule the task that executes the end method.
/// The TaskCreationOptions value that controls the behavior of the
/// created Task .
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// A Task that represents the
/// asynchronous operation.
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable
public Task FromAsync(
IAsyncResult asyncResult,
Func endMethod,
TaskCreationOptions creationOptions,
TaskScheduler scheduler)
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return FromAsyncImpl(asyncResult, endMethod, creationOptions, scheduler, ref stackMark);
}
// internal overload that supports StackCrawlMark
// We also need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory instance.
internal static Task FromAsyncImpl(
IAsyncResult asyncResult,
Func endMethod,
TaskCreationOptions creationOptions,
TaskScheduler scheduler,
ref StackCrawlMark stackMark)
{
if (asyncResult == null)
throw new ArgumentNullException("asyncResult");
if (endMethod == null)
throw new ArgumentNullException("endMethod");
if (scheduler == null)
throw new ArgumentNullException("scheduler");
TaskFactory.CheckFromAsyncOptions(creationOptions, false);
TaskCompletionSource tcs = new TaskCompletionSource(creationOptions);
// Just specify this task as detached. No matter what happens, we want endMethod
// to be called -- even if the parent is canceled. So we don't want to flow
// RespectParentCancellation.
Task t = new Task(delegate
{
FromAsyncCoreLogic(asyncResult, endMethod, tcs);
},
(object)null, Task.InternalCurrent,
CancellationToken.None, TaskCreationOptions.None, InternalTaskOptions.None, null, ref stackMark);
if (asyncResult.IsCompleted)
{
try { t.RunSynchronously(scheduler); }
catch (Exception e) { tcs.TrySetException(e); } // catch and log any scheduler exceptions
}
else
{
ThreadPool.RegisterWaitForSingleObject(
asyncResult.AsyncWaitHandle,
delegate
{
try { t.RunSynchronously(scheduler); }
catch (Exception e) { tcs.TrySetException(e); } // catch and log any scheduler exceptions
},
null,
Timeout.Infinite,
true);
}
return tcs.Task;
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod, object state)
{
return FromAsyncImpl(beginMethod, endMethod, state, m_defaultCreationOptions);
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// The TaskCreationOptions value that controls the behavior of the
/// created Task .
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod, object state, TaskCreationOptions creationOptions)
{
return FromAsyncImpl(beginMethod, endMethod, state, creationOptions);
}
// We need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory instance.
internal static Task FromAsyncImpl(
Func beginMethod,
Func endMethod, object state, TaskCreationOptions creationOptions)
{
if (beginMethod == null)
throw new ArgumentNullException("beginMethod");
if (endMethod == null)
throw new ArgumentNullException("endMethod");
TaskFactory.CheckFromAsyncOptions(creationOptions, true);
TaskCompletionSource tcs = new TaskCompletionSource(state, creationOptions);
try
{
beginMethod(iar =>
{
FromAsyncCoreLogic(iar, endMethod, tcs);
}, state);
}
catch
{
// Make sure we don't leave tcs "dangling".
tcs.TrySetResult(default(TResult));
throw;
}
return tcs.Task;
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The type of the first argument passed to the delegate.
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// The first argument passed to the
/// delegate.
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod,
TArg1 arg1, object state)
{
return FromAsyncImpl(beginMethod, endMethod, arg1, state, m_defaultCreationOptions);
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The type of the first argument passed to the delegate.
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// The first argument passed to the
/// delegate.
/// The TaskCreationOptions value that controls the behavior of the
/// created Task .
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument specifies an invalid TaskCreationOptions
/// value.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func endMethod,
TArg1 arg1, object state, TaskCreationOptions creationOptions)
{
return FromAsyncImpl(beginMethod, endMethod, arg1, state, creationOptions);
}
// We need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory instance.
internal static Task FromAsyncImpl(
Func beginMethod,
Func endMethod,
TArg1 arg1, object state, TaskCreationOptions creationOptions)
{
if (beginMethod == null)
throw new ArgumentNullException("beginMethod");
if (endMethod == null)
throw new ArgumentNullException("endMethod");
TaskFactory.CheckFromAsyncOptions(creationOptions, true);
TaskCompletionSource tcs = new TaskCompletionSource(state, creationOptions);
try
{
beginMethod(arg1, iar =>
{
FromAsyncCoreLogic(iar, endMethod, tcs);
}, state);
}
catch
{
// Make sure we don't leave tcs "dangling".
tcs.TrySetResult(default(TResult));
throw;
}
return tcs.Task;
}
///
/// Creates a Task that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
///
/// The type of the first argument passed to the delegate.
/// The type of the second argument passed to
/// delegate.
/// The delegate that begins the asynchronous operation.
/// The delegate that ends the asynchronous operation.
/// The first argument passed to the
/// delegate.
/// The second argument passed to the
/// delegate.
/// An object containing data to be used by the
/// delegate.
/// The exception that is thrown when the
/// argument is null.
/// The exception that is thrown when the
/// argument is null.
/// The created Task that
/// represents the asynchronous operation.
///
/// This method throws any exceptions thrown by the .
///
public Task FromAsync(
Func beginMethod,
Func