Export (0) Print
Expand All

TaskFactory Class

Provides support for creating and scheduling Task objects.

System.Object
  System.Threading.Tasks.TaskFactory

Namespace:  System.Threading.Tasks
Assemblies:   mscorlib (in mscorlib.dll)
  System.Threading.Tasks (in System.Threading.Tasks.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
	ExternalThreading = true)]
public class TaskFactory

The TaskFactory type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibraryTaskFactory()Initializes a TaskFactory instance with the default configuration.
Public methodSupported by Portable Class LibraryTaskFactory(CancellationToken)Initializes a TaskFactory instance with the specified configuration.
Public methodSupported by Portable Class LibraryTaskFactory(TaskScheduler)Initializes a TaskFactory instance with the specified configuration.
Public methodSupported by Portable Class LibraryTaskFactory(TaskCreationOptions, TaskContinuationOptions)Initializes a TaskFactory instance with the specified configuration.
Public methodSupported by Portable Class LibraryTaskFactory(CancellationToken, TaskCreationOptions, TaskContinuationOptions, TaskScheduler)Initializes a TaskFactory instance with the specified configuration.
Top

  NameDescription
Public propertySupported by Portable Class LibraryCancellationTokenGets the default cancellation token for this task factory.
Public propertySupported by Portable Class LibraryContinuationOptionsGets the default task continuation options for this task factory.
Public propertySupported by Portable Class LibraryCreationOptionsGets the default task creation options for this task factory.
Public propertySupported by Portable Class LibrarySchedulerGets the default task scheduler for this task factory.
Top

  NameDescription
Public methodSupported by Portable Class LibraryContinueWhenAll(Task[], Action<Task[]>)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll(Task[], Action<Task[]>, CancellationToken)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll(Task[], Action<Task[]>, TaskContinuationOptions)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll(Task[], Action<Task[]>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TResult>(Task[], Func<Task[], TResult>)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[], Action<Task<TAntecedentResult>[]>)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TResult>(Task[], Func<Task[], TResult>, CancellationToken)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TResult>(Task[], Func<Task[], TResult>, TaskContinuationOptions)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[], Action<Task<TAntecedentResult>[]>, CancellationToken)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[], Action<Task<TAntecedentResult>[]>, TaskContinuationOptions)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TResult>(Task[], Func<Task[], TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[], Action<Task<TAntecedentResult>[]>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>[], TResult>)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>[], TResult>, CancellationToken)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>[], TResult>, TaskContinuationOptions)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>[], TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation task that starts when a set of specified tasks has completed.
Public methodSupported by Portable Class LibraryContinueWhenAny(Task[], Action<Task>)Creates a continuation Task that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny(Task[], Action<Task>, CancellationToken)Creates a continuation Task that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny(Task[], Action<Task>, TaskContinuationOptions)Creates a continuation Task that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny(Task[], Action<Task>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation Task that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TResult>(Task[], Func<Task, TResult>)Creates a continuation Task<TResult> that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[], Action<Task<TAntecedentResult>>)Creates a continuation Task that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TResult>(Task[], Func<Task, TResult>, CancellationToken)Creates a continuation Task<TResult> that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TResult>(Task[], Func<Task, TResult>, TaskContinuationOptions)Creates a continuation Task<TResult> that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[], Action<Task<TAntecedentResult>>, CancellationToken)Creates a continuation Task that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[], Action<Task<TAntecedentResult>>, TaskContinuationOptions)Creates a continuation Task that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TResult>(Task[], Func<Task, TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation Task<TResult> that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[], Action<Task<TAntecedentResult>>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation Task that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>, TResult>)Creates a continuation Task<TResult> that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>, TResult>, CancellationToken)Creates a continuation Task<TResult> that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>, TResult>, TaskContinuationOptions)Creates a continuation Task<TResult> that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>, TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation Task<TResult> that will be started upon the completion of any Task in the provided set.
Public methodSupported by Portable Class LibraryEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Portable Class LibraryFromAsync(IAsyncResult, Action<IAsyncResult>)Creates a Task that executes an end method action when a specified IAsyncResult completes.
Public methodSupported by Portable Class LibraryFromAsync(Func<AsyncCallback, Object, IAsyncResult>, Action<IAsyncResult>, Object)Creates a Task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync(IAsyncResult, Action<IAsyncResult>, TaskCreationOptions)Creates a Task that executes an end method action when a specified IAsyncResult completes.
Public methodSupported by Portable Class LibraryFromAsync(Func<AsyncCallback, Object, IAsyncResult>, Action<IAsyncResult>, Object, TaskCreationOptions)Creates a Task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync(IAsyncResult, Action<IAsyncResult>, TaskCreationOptions, TaskScheduler)Creates a Task that executes an end method action when a specified IAsyncResult completes.
Public methodSupported by Portable Class LibraryFromAsync<TResult>(IAsyncResult, Func<IAsyncResult, TResult>)Creates a Task<TResult> that executes an end method function when a specified IAsyncResult completes.
Public methodSupported by Portable Class LibraryFromAsync<TResult>(Func<AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, Object)Creates a Task<TResult> that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TResult>(IAsyncResult, Func<IAsyncResult, TResult>, TaskCreationOptions)Creates a Task<TResult> that executes an end method function when a specified IAsyncResult completes.
Public methodSupported by Portable Class LibraryFromAsync<TResult>(Func<AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, Object, TaskCreationOptions)Creates a Task<TResult> that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TArg1>(Func<TArg1, AsyncCallback, Object, IAsyncResult>, Action<IAsyncResult>, TArg1, Object)Creates a Task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TResult>(IAsyncResult, Func<IAsyncResult, TResult>, TaskCreationOptions, TaskScheduler)Creates a Task<TResult> that executes an end method function when a specified IAsyncResult completes.
Public methodSupported by Portable Class LibraryFromAsync<TArg1>(Func<TArg1, AsyncCallback, Object, IAsyncResult>, Action<IAsyncResult>, TArg1, Object, TaskCreationOptions)Creates a Task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TArg1, TResult>(Func<TArg1, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, TArg1, Object)Creates a Task<TResult> that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TArg1, TResult>(Func<TArg1, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, TArg1, Object, TaskCreationOptions)Creates a Task<TResult> that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TArg1, TArg2>(Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult>, Action<IAsyncResult>, TArg1, TArg2, Object)Creates a Task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TArg1, TArg2>(Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult>, Action<IAsyncResult>, TArg1, TArg2, Object, TaskCreationOptions)Creates a Task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TArg1, TArg2, TResult>(Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, TArg1, TArg2, Object)Creates a Task<TResult> that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TArg1, TArg2, TResult>(Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, TArg1, TArg2, Object, TaskCreationOptions)Creates a Task<TResult> that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TArg1, TArg2, TArg3>(Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult>, Action<IAsyncResult>, TArg1, TArg2, TArg3, Object)Creates a Task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TArg1, TArg2, TArg3>(Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult>, Action<IAsyncResult>, TArg1, TArg2, TArg3, Object, TaskCreationOptions)Creates a Task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TArg1, TArg2, TArg3, TResult>(Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, TArg1, TArg2, TArg3, Object)Creates a Task<TResult> that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryFromAsync<TArg1, TArg2, TArg3, TResult>(Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, TArg1, TArg2, TArg3, Object, TaskCreationOptions)Creates a Task<TResult> that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.
Public methodSupported by Portable Class LibraryGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Portable Class LibraryStartNew(Action)Creates and starts a task.
Public methodSupported by Portable Class LibraryStartNew(Action, CancellationToken)Creates and starts a Task.
Public methodSupported by Portable Class LibraryStartNew(Action, TaskCreationOptions)Creates and starts a Task.
Public methodSupported by Portable Class LibraryStartNew(Action<Object>, Object)Creates and starts a Task.
Public methodSupported by Portable Class LibraryStartNew(Action<Object>, Object, CancellationToken)Creates and starts a Task.
Public methodSupported by Portable Class LibraryStartNew(Action<Object>, Object, TaskCreationOptions)Creates and starts a Task.
Public methodSupported by Portable Class LibraryStartNew(Action, CancellationToken, TaskCreationOptions, TaskScheduler)Creates and starts a Task.
Public methodSupported by Portable Class LibraryStartNew(Action<Object>, Object, CancellationToken, TaskCreationOptions, TaskScheduler)Creates and starts a Task.
Public methodSupported by Portable Class LibraryStartNew<TResult>(Func<TResult>)Creates and starts a Task<TResult>.
Public methodSupported by Portable Class LibraryStartNew<TResult>(Func<TResult>, CancellationToken)Creates and starts a Task<TResult>.
Public methodSupported by Portable Class LibraryStartNew<TResult>(Func<TResult>, TaskCreationOptions)Creates and starts a Task<TResult>.
Public methodSupported by Portable Class LibraryStartNew<TResult>(Func<Object, TResult>, Object)Creates and starts a Task<TResult>.
Public methodSupported by Portable Class LibraryStartNew<TResult>(Func<Object, TResult>, Object, CancellationToken)Creates and starts a Task<TResult>.
Public methodSupported by Portable Class LibraryStartNew<TResult>(Func<Object, TResult>, Object, TaskCreationOptions)Creates and starts a Task<TResult>.
Public methodSupported by Portable Class LibraryStartNew<TResult>(Func<TResult>, CancellationToken, TaskCreationOptions, TaskScheduler)Creates and starts a Task<TResult>.
Public methodSupported by Portable Class LibraryStartNew<TResult>(Func<Object, TResult>, Object, CancellationToken, TaskCreationOptions, TaskScheduler)Creates and starts a Task<TResult>.
Public methodSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The .NET Framework provides two factories for creating and scheduling tasks:

  • The TaskFactory class, which creates Task and Task<TResult> objects. You can call the overloads of this method to create and execute a task that requires non-default arguments.

    Caution noteCaution

    Starting with the .NET Framework 4.5, the Task.Run method provides the easiest way to create a task with default configuration values and start it immediately.

  • The TaskFactory<TResult> class, which creates Task<TResult> objects.

The TaskFactory class allows you to do the following:

  • Create a task and start it immediately by calling the StartNew method.

    Caution noteCaution

    Starting with the .NET Framework 4.5, the Task.Run method provides the easiest way to create a task with default configuration values and start it immediately.

  • Create a task that starts when any one of the tasks in an array has completed by calling the ContinueWhenAny method.

  • Create a task that starts when all the tasks in an array have completed by calling the ContinueWhenAll method.

The static Task<TResult>.Factory property returns a default TaskFactory<TResult> object. You can also call one of the TaskFactory class constructors to configure the Task objects that the TaskFactory class creates. The following example configures a new TaskFactory object to create tasks that have a specified cancellation token, task creation options, continuation options, and a customized task scheduler.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

class Example
{
   static CancellationTokenSource cts = new CancellationTokenSource();

   static TaskFactory factory = new TaskFactory(
      cts.Token,
      TaskCreationOptions.PreferFairness,
      TaskContinuationOptions.ExecuteSynchronously,
      new CustomScheduler());

   static void Main()
   {
      var t2 = factory.StartNew(() => DoWork());
      cts.Dispose();
   }

   static void DoWork() {/*...*/ }
}

In most cases, you do not have to instantiate a new TaskFactory instance. Instead, you can use the Task.Factory property, which returns a factory object that uses default values. You can then call its methods to start new tasks or define task continuations. For an illustration, see the example.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following example uses the static Factory property to make two calls to the TaskFactory.StartNew method. The first populates an array with the names of files in the user's MyDocuments directory, while the second populates an array with the names of subdirectories of the user's MyDocuments directory. It then calls the TaskFactory.ContinueWhenAll(Task[], Action<Task[]>) method, which displays information about the number of files and directories in the two arrays after the first two tasks have completed execution.

using System;
using System.IO;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Task[] tasks = new Task[2];
      String[] files = null;
      String[] dirs = null;
      String docsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

      tasks[0] = Task.Factory.StartNew( () => files = Directory.GetFiles(docsDirectory));
      tasks[1] = Task.Factory.StartNew( () => dirs = Directory.GetDirectories(docsDirectory));

      Task.Factory.ContinueWhenAll(tasks, completedTasks => {
                                             Console.WriteLine("{0} contains: ", docsDirectory);
                                             Console.WriteLine("   {0} subdirectories", dirs.Length);
                                             Console.WriteLine("   {0} files", files.Length);
                                          } );
   }
}
// The example displays output like the following: 
//       C:\Users\<username>\Documents contains: 
//          24 subdirectories 
//          16 files

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

All public and protected members of TaskFactory are thread-safe and may be used concurrently from multiple threads.

Show:
© 2015 Microsoft