TaskFactory<TResult> Class
Provides support for creating and scheduling Task<TResult> objects.
Assembly: mscorlib (in mscorlib.dll)
generic<typename TResult> [HostProtectionAttribute(SecurityAction::LinkDemand, Synchronization = true, ExternalThreading = true)] public ref class TaskFactory
Type Parameters
- TResult
The return value of the Task<TResult> objects that the methods of this class create.
| Name | Description | |
|---|---|---|
![]() | TaskFactory<TResult>() | Initializes a TaskFactory<TResult> instance with the default configuration. |
![]() | TaskFactory<TResult>(CancellationToken) | Initializes a TaskFactory<TResult> instance with the default configuration. |
![]() | TaskFactory<TResult>(CancellationToken, TaskCreationOptions, TaskContinuationOptions, TaskScheduler^) | Initializes a TaskFactory<TResult> instance with the specified configuration. |
![]() | TaskFactory<TResult>(TaskCreationOptions, TaskContinuationOptions) | Initializes a TaskFactory<TResult> instance with the specified configuration. |
![]() | TaskFactory<TResult>(TaskScheduler^) | Initializes a TaskFactory<TResult> instance with the specified configuration. |
| Name | Description | |
|---|---|---|
![]() | CancellationToken | Gets the default cancellation token for this task factory. |
![]() | ContinuationOptions | Gets the TaskContinuationOptions enumeration value for this task factory. |
![]() | CreationOptions | Gets the TaskCreationOptions enumeration value for this task factory. |
![]() | Scheduler | Gets the task scheduler for this task factory. |
The .NET Framework provides two factories for creating and scheduling tasks:
The TaskFactory class, which creates Task and Task<TResult> objects.
The TaskFactory<TResult> class, which creates Task<TResult> objects.
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.
Warning 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.
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(array<Task^>^, Action<array<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.
Available since 8
.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.


