TaskFactory Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Provides support for creating and scheduling Task
generic
public ref class TaskFactory
public class TaskFactory
type TaskFactory<'Result> = class
Public Class TaskFactory(Of TResult)
Type Parameters
- TResult
The return value of the Task
- Inheritance
-
TaskFactory
Examples
The following example uses the static Factory property to make two calls to the TaskFactory
using System;
using System.IO;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
Task[] tasks = new Task[2];
String docsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
tasks[0] = Task.Factory.StartNew( () => Directory.GetFiles(docsDirectory));
tasks[1] = Task.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\\Documents contains:
// 24 subdirectories
// 16 files
Imports System.IO
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim tasks(1) As Task(Of String())
Dim docsDirectory As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
tasks(0) = Task(Of String()).Factory.StartNew( Function()Directory.GetFiles(docsDirectory) )
'' End Sub )
tasks(1) = Task(Of String()).Factory.StartNew( Function() Directory.GetDirectories(docsDirectory) )
'' End Sub )
Task.Factory.ContinueWhenAll(tasks, Sub(completedTasks)
Console.WriteLine("{0} contains: ", docsDirectory)
Console.WriteLine(" {0} subdirectories", tasks(1).Result.Length)
Console.WriteLine(" {0} files", tasks(0).Result.Length)
End Sub)
End Sub
End Module
' The example displays output like the following:
' C:\Users\\Documents contains:
' 24 subdirectories
' 16 files
Remarks
.NET provides two factories for creating and scheduling tasks:
The TaskFactory class, which creates Task and Task
objects. The TaskFactory
class, which creates Task objects.
The TaskFactory
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 .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 method.
Create a task that starts when all the tasks in an array have completed by calling the ContinueWhenAll or ContinueWhenAll method.
The static Task
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
public 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 int DoWork()
{
/*...*/
return DateTime.Now.Hour <= 12 ? 1 : 2;
}
}
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim cts As New CancellationTokenSource()
Dim factory As New TaskFactory(Of Integer)(
cts.Token,
TaskCreationOptions.PreferFairness,
TaskContinuationOptions.ExecuteSynchronously,
New CustomScheduler())
Dim t2 = factory.StartNew(Function() DoWork())
cts.Dispose()
End Sub
Function DoWork() As Integer
Return If(Date.Now.Hour <= 12, 1, 2)
End Function
End Module
In most cases, you do not have to instantiate a new TaskFactory
Constructors
| Name | Description |
|---|---|
|
TaskFactory |
Initializes a TaskFactory |
|
TaskFactory |
Initializes a TaskFactory |
|
TaskFactory |
Initializes a TaskFactory |
|
TaskFactory |
Initializes a TaskFactory |
|
TaskFactory |
Initializes a TaskFactory |
Properties
| 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. |
Methods
| Name | Description |
|---|---|
|
ContinueWhenAll(Task[], Func |
Creates a continuation task that will be started upon the completion of a set of provided Tasks. |
|
ContinueWhenAll(Task[], Func |
Creates a continuation task that will be started upon the completion of a set of provided tasks. |
|
ContinueWhenAll(Task[], Func |
Creates a continuation task that will be started upon the completion of a set of provided Tasks. |
|
ContinueWhenAll(Task[], Func |
Creates a continuation task that will be started upon the completion of a set of provided tasks. |
|
ContinueWhenAll |
Creates a continuation task that will be started upon the completion of a set of provided tasks. |
|
ContinueWhenAll |
Creates a continuation task that will be started upon the completion of a set of provided tasks. |
|
ContinueWhenAll |
Creates a continuation task that will be started upon the completion of a set of provided tasks. |
|
ContinueWhenAll |
Creates a continuation task that will be started upon the completion of a set of provided tasks. |
|
ContinueWhenAny(Task[], Func |
Creates a continuation task that will be started upon the completion of any task in the provided set. |
|
ContinueWhenAny(Task[], Func |
Creates a continuation task that will be started upon the completion of any task in the provided set. |
|
ContinueWhenAny(Task[], Func |
Creates a continuation task that will be started upon the completion of any task in the provided set. |
|
ContinueWhenAny(Task[], Func |
Creates a continuation task that will be started upon the completion of any task in the provided set. |
|
ContinueWhenAny |
Creates a continuation task that will be started upon the completion of any task in the provided set. |
|
ContinueWhenAny |
Creates a continuation task that will be started upon the completion of any task in the provided set. |
|
ContinueWhenAny |
Creates a continuation task that will be started upon the completion of any task in the provided set. |
|
ContinueWhenAny |
Creates a continuation task that will be started upon the completion of any task in the provided set. |
| Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
|
FromAsync(Func |
Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern. |
|
FromAsync(Func |
Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern. |
|
FromAsync(IAsyncResult, Func |
Creates a task that executes an end method function when a specified IAsyncResult completes. |
|
FromAsync(IAsyncResult, Func |
Creates a task that executes an end method function when a specified IAsyncResult completes. |
|
FromAsync(IAsyncResult, Func |
Creates a task that executes an end method function when a specified IAsyncResult completes. |
|
FromAsync |
Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern. |
|
FromAsync |
Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern. |
|
FromAsync |
Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern. |
|
FromAsync |
Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern. |
|
FromAsync |
Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern. |
|
FromAsync |
Creates a task that represents a pair of begin and end methods that conform to the Asynchronous Programming Model pattern. |
| GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
| GetType() |
Gets the Type of the current instance. (Inherited from Object) |
| MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
| StartNew(Func |
Creates and starts a task. |
| StartNew(Func |
Creates and starts a task. |
| StartNew(Func |
Creates and starts a task. |
| StartNew(Func |
Creates and starts a task. |
|
StartNew(Func |
Creates and starts a task. |
|
StartNew(Func |
Creates and starts a task. |
|
StartNew(Func |
Creates and starts a task. |
|
StartNew(Func |
Creates and starts a task. |
| ToString() |
Returns a string that represents the current object. (Inherited from Object) |
Applies to
Thread Safety
All public and protected members of TaskFactory