TaskFactory(TResult) Class

TaskFactory<TResult> Class

 

Provides support for creating and scheduling Task<TResult> objects.

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

System.Object
  System.Threading.Tasks.TaskFactory<TResult>

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

Type Parameters

TResult

The return value of the Task<TResult> objects that the methods of this class create.

NameDescription
System_CAPS_pubmethodTaskFactory<TResult>()

Initializes a TaskFactory<TResult> instance with the default configuration.

System_CAPS_pubmethodTaskFactory<TResult>(CancellationToken)

Initializes a TaskFactory<TResult> instance with the default configuration.

System_CAPS_pubmethodTaskFactory<TResult>(CancellationToken, TaskCreationOptions, TaskContinuationOptions, TaskScheduler)

Initializes a TaskFactory<TResult> instance with the specified configuration.

System_CAPS_pubmethodTaskFactory<TResult>(TaskCreationOptions, TaskContinuationOptions)

Initializes a TaskFactory<TResult> instance with the specified configuration.

System_CAPS_pubmethodTaskFactory<TResult>(TaskScheduler)

Initializes a TaskFactory<TResult> instance with the specified configuration.

NameDescription
System_CAPS_pubpropertyCancellationToken

Gets the default cancellation token for this task factory.

System_CAPS_pubpropertyContinuationOptions

Gets the TaskContinuationOptions enumeration value for this task factory.

System_CAPS_pubpropertyCreationOptions

Gets the TaskCreationOptions enumeration value for this task factory.

System_CAPS_pubpropertyScheduler

Gets the task scheduler for this task factory.

NameDescription
System_CAPS_pubmethodContinueWhenAll(Task[], Func<Task[], TResult>)

Creates a continuation task that will be started upon the completion of a set of provided tasks.

System_CAPS_pubmethodContinueWhenAll(Task[], Func<Task[], TResult>, CancellationToken)

Creates a continuation task that will be started upon the completion of a set of provided tasks.

System_CAPS_pubmethodContinueWhenAll(Task[], Func<Task[], TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Creates a continuation task that will be started upon the completion of a set of provided Tasks.

System_CAPS_pubmethodContinueWhenAll(Task[], Func<Task[], TResult>, TaskContinuationOptions)

Creates a continuation task that will be started upon the completion of a set of provided Tasks.

System_CAPS_pubmethodContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>[], TResult>)

Creates a continuation task that will be started upon the completion of a set of provided tasks.

System_CAPS_pubmethodContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>[], TResult>, CancellationToken)

Creates a continuation task that will be started upon the completion of a set of provided tasks.

System_CAPS_pubmethodContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>[], TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Creates a continuation task that will be started upon the completion of a set of provided tasks.

System_CAPS_pubmethodContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>[], TResult>, TaskContinuationOptions)

Creates a continuation task that will be started upon the completion of a set of provided tasks.

System_CAPS_pubmethodContinueWhenAny(Task[], Func<Task, TResult>)

Creates a continuation task that will be started upon the completion of any task in the provided set.

System_CAPS_pubmethodContinueWhenAny(Task[], Func<Task, TResult>, CancellationToken)

Creates a continuation task that will be started upon the completion of any task in the provided set.

System_CAPS_pubmethodContinueWhenAny(Task[], Func<Task, TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Creates a continuation task that will be started upon the completion of any task in the provided set.

System_CAPS_pubmethodContinueWhenAny(Task[], Func<Task, TResult>, TaskContinuationOptions)

Creates a continuation task that will be started upon the completion of any task in the provided set.

System_CAPS_pubmethodContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>, TResult>)

Creates a continuation task that will be started upon the completion of any task in the provided set.

System_CAPS_pubmethodContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>, TResult>, CancellationToken)

Creates a continuation task that will be started upon the completion of any task in the provided set.

System_CAPS_pubmethodContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>, TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Creates a continuation task that will be started upon the completion of any task in the provided set.

System_CAPS_pubmethodContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>, TResult>, TaskContinuationOptions)

Creates a continuation task that will be started upon the completion of any task in the provided set.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodFromAsync(Func<AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, Object)

Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.

System_CAPS_pubmethodFromAsync(Func<AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, Object, TaskCreationOptions)

Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.

System_CAPS_pubmethodFromAsync(IAsyncResult, Func<IAsyncResult, TResult>)

Creates a task that executes an end method function when a specified IAsyncResult completes.

System_CAPS_pubmethodFromAsync(IAsyncResult, Func<IAsyncResult, TResult>, TaskCreationOptions)

Creates a task that executes an end method function when a specified IAsyncResult completes.

System_CAPS_pubmethodFromAsync(IAsyncResult, Func<IAsyncResult, TResult>, TaskCreationOptions, TaskScheduler)

Creates a task that executes an end method function when a specified IAsyncResult completes.

System_CAPS_pubmethodFromAsync<TArg1>(Func<TArg1, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, TArg1, Object)

Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.

System_CAPS_pubmethodFromAsync<TArg1>(Func<TArg1, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, TArg1, Object, TaskCreationOptions)

Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.

System_CAPS_pubmethodFromAsync<TArg1, TArg2>(Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, TArg1, TArg2, Object)

Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.

System_CAPS_pubmethodFromAsync<TArg1, TArg2>(Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, TArg1, TArg2, Object, TaskCreationOptions)

Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.

System_CAPS_pubmethodFromAsync<TArg1, TArg2, TArg3>(Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, TArg1, TArg2, TArg3, Object)

Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern.

System_CAPS_pubmethodFromAsync<TArg1, TArg2, TArg3>(Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>, 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.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodStartNew(Func<TResult>)

Creates and starts a task.

System_CAPS_pubmethodStartNew(Func<TResult>, CancellationToken)

Creates and starts a task.

System_CAPS_pubmethodStartNew(Func<TResult>, CancellationToken, TaskCreationOptions, TaskScheduler)

Creates and starts a task.

System_CAPS_pubmethodStartNew(Func<TResult>, TaskCreationOptions)

Creates and starts a task.

System_CAPS_pubmethodStartNew(Func<Object, TResult>, Object)

Creates and starts a task.

System_CAPS_pubmethodStartNew(Func<Object, TResult>, Object, CancellationToken)

Creates and starts a task.

System_CAPS_pubmethodStartNew(Func<Object, TResult>, Object, CancellationToken, TaskCreationOptions, TaskScheduler)

Creates and starts a task.

System_CAPS_pubmethodStartNew(Func<Object, TResult>, Object, TaskCreationOptions)

Creates and starts a task.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

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

The TaskFactory<TResult> class allows you to do the following:

  • Create a task and start it immediately by calling the StartNew method. You can call the overloads of this method to create and execute a task that requires non-default arguments.

    System_CAPS_warningWarning

    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 or ContinueWhenAny<TResult> method.

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

The static Task<TResult>.Factory property returns a default TaskFactory<TResult> object. You can also call one of the TaskFactory<TResult> class constructors to configure the Task<TResult> objects that the TaskFactory<TResult> class creates. The following example configures a new TaskFactory<TResult> 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;

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

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

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

   static int DoWork()
   {
       /*...*/
       return DateTime.Now.Hour <= 12 ?  1 : 2;
    }
}

In most cases, you do not have to instantiate a new TaskFactory<TResult> instance. Instead, you can use the static Task<TResult>.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.

The following example uses the static Factory property to make two calls to the TaskFactory<TResult>.StartNew method. The first task returns a string array that is populated with the names of files in the user's MyDocuments directory, while the second returns a string array that is populated 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 arrays returned by the two tasks after they have completed execution.

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

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

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

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

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 5.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1

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

Return to top
Show:
© 2016 Microsoft