TaskFactory<TResult> 类
定义
提供对创建和计划 Task<TResult> 对象的支持。Provides support for creating and scheduling Task<TResult> objects.
generic <typename TResult>
public ref class TaskFactory
public class TaskFactory<TResult>
type TaskFactory<'Result> = class
Public Class TaskFactory(Of TResult)
类型参数
- TResult
此类的方法创建的 Task<TResult> 对象的返回值。The return value of the Task<TResult> objects that the methods of this class create.
- 继承
-
TaskFactory<TResult>
示例
下面的示例使用静态 Factory 属性对方法进行两次调用 TaskFactory<TResult>.StartNew 。The following example uses the static Factory property to make two calls to the TaskFactory<TResult>.StartNew method. 第一项任务返回一个字符串数组,该字符串数组使用用户的 MyDocuments 目录中的文件的名称进行填充,第二个任务返回一个字符串数组,该数组使用用户的 MyDocuments 目录的子目录名称填充。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. 然后,它调用 TaskFactory.ContinueWhenAll(Task[], Action<Task[]>) 方法,该方法显示两个任务完成执行后返回的数组中的文件和目录的数量信息。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
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\<username>\Documents contains:
' 24 subdirectories
' 16 files
注解
.NET Framework 提供了两个用于创建和计划任务的工厂:The .NET Framework provides two factories for creating and scheduling tasks:
TaskFactory类,该类创建 Task 和 Task<TResult> 对象。The TaskFactory class, which creates Task and Task<TResult> objects.
TaskFactory<TResult>类,该类用于创建 Task<TResult> 对象。The TaskFactory<TResult> class, which creates Task<TResult> objects.
TaskFactory<TResult>类允许你执行以下操作:The TaskFactory<TResult> class allows you to do the following:
创建一个任务,并通过调用方法立即启动该任务 StartNew 。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.
创建一个任务,该任务在数组中的任一任务通过调用或方法完成后开始 ContinueWhenAny ContinueWhenAny 。Create a task that starts when any one of the tasks in an array has completed by calling the ContinueWhenAny or ContinueWhenAny method.
创建一个任务,该任务将在数组中的所有任务通过调用或方法完成后启动 ContinueWhenAll ContinueWhenAll 。Create a task that starts when all the tasks in an array have completed by calling the ContinueWhenAll or ContinueWhenAll method.
静态 Task<TResult>.Factory 属性返回默认 TaskFactory<TResult> 对象。The static Task<TResult>.Factory property returns a default TaskFactory<TResult> object. 还可以调用 TaskFactory<TResult> 类构造函数之一来配置 Task<TResult> TaskFactory<TResult> 类创建的对象。You can also call one of the TaskFactory<TResult> class constructors to configure the Task<TResult> objects that the TaskFactory<TResult> class creates. 下面的示例配置一个新的 TaskFactory<TResult> 对象,以创建具有指定的取消标记、任务创建选项、延续选项和自定义任务计划程序的任务。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;
}
}
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
在大多数情况下,您不必实例化新的 TaskFactory<TResult> 实例。In most cases, you do not have to instantiate a new TaskFactory<TResult> instance. 相反,可以使用 static Task<TResult>.Factory 属性,该属性返回使用默认值的工厂对象。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.
构造函数
TaskFactory<TResult>() |
使用默认配置初始化 TaskFactory<TResult> 实例。Initializes a TaskFactory<TResult> instance with the default configuration. |
TaskFactory<TResult>(CancellationToken) |
使用默认配置初始化 TaskFactory<TResult> 实例。Initializes a TaskFactory<TResult> instance with the default configuration. |
TaskFactory<TResult>(CancellationToken, TaskCreationOptions, TaskContinuationOptions, TaskScheduler) |
使用指定配置初始化 TaskFactory<TResult> 实例。Initializes a TaskFactory<TResult> instance with the specified configuration. |
TaskFactory<TResult>(TaskCreationOptions, TaskContinuationOptions) |
使用指定配置初始化 TaskFactory<TResult> 实例。Initializes a TaskFactory<TResult> instance with the specified configuration. |
TaskFactory<TResult>(TaskScheduler) |
使用指定配置初始化 TaskFactory<TResult> 实例。Initializes a TaskFactory<TResult> instance with the specified configuration. |
属性
CancellationToken |
获取此任务工厂的默认取消标记。Gets the default cancellation token for this task factory. |
ContinuationOptions |
获取此任务工厂的 TaskContinuationOptions 枚举值。Gets the TaskContinuationOptions enumeration value for this task factory. |
CreationOptions |
获取此任务工厂的 TaskCreationOptions 枚举值。Gets the TaskCreationOptions enumeration value for this task factory. |
Scheduler |
获取此任务工厂的任务计划程序。Gets the task scheduler for this task factory. |
方法
ContinueWhenAll(Task[], Func<Task[],TResult>) |
创建一个延续任务,它将在提供的一组任务完成后马上开始。Creates a continuation task that will be started upon the completion of a set of provided tasks. |
ContinueWhenAll(Task[], Func<Task[],TResult>, CancellationToken) |
创建一个延续任务,它将在提供的一组任务完成后马上开始。Creates a continuation task that will be started upon the completion of a set of provided tasks. |
ContinueWhenAll(Task[], Func<Task[],TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) |
创建一个延续任务,它将在提供的一组任务完成后马上开始。Creates a continuation task that will be started upon the completion of a set of provided Tasks. |
ContinueWhenAll(Task[], Func<Task[],TResult>, TaskContinuationOptions) |
创建一个延续任务,它将在提供的一组任务完成后马上开始。Creates a continuation task that will be started upon the completion of a set of provided Tasks. |
ContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[], Func<Task<TAntecedentResult>[],TResult>) |
创建一个延续任务,它将在提供的一组任务完成后马上开始。Creates a continuation task that will be started upon the completion of a set of provided tasks. |
ContinueWhenAll<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. |
ContinueWhenAll<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. |
ContinueWhenAll<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. |
ContinueWhenAny(Task[], Func<Task,TResult>) |
创建一个延续任务,它将在提供的组中的任何任务完成后马上开始。Creates a continuation task that will be started upon the completion of any task in the provided set. |
ContinueWhenAny(Task[], Func<Task,TResult>, CancellationToken) |
创建一个延续任务,它将在提供的组中的任何任务完成后马上开始。Creates a continuation task that will be started upon the completion of any task in the provided set. |
ContinueWhenAny(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. |
ContinueWhenAny(Task[], Func<Task,TResult>, TaskContinuationOptions) |
创建一个延续任务,它将在提供的组中的任何任务完成后马上开始。Creates a continuation task that will be started upon the completion of any task in the provided set. |
ContinueWhenAny<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. |
ContinueWhenAny<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. |
ContinueWhenAny<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. |
ContinueWhenAny<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. |
Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
FromAsync(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. |
FromAsync(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. |
FromAsync(IAsyncResult, Func<IAsyncResult,TResult>) |
创建一个任务,它在指定的 IAsyncResult 完成时执行一个结束方法函数。Creates a task that executes an end method function when a specified IAsyncResult completes. |
FromAsync(IAsyncResult, Func<IAsyncResult,TResult>, TaskCreationOptions) |
创建一个任务,它在指定的 IAsyncResult 完成时执行一个结束方法函数。Creates a task that executes an end method function when a specified IAsyncResult completes. |
FromAsync(IAsyncResult, Func<IAsyncResult,TResult>, TaskCreationOptions, TaskScheduler) |
创建一个任务,它在指定的 IAsyncResult 完成时执行一个结束方法函数。Creates a task that executes an end method function when a specified IAsyncResult completes. |
FromAsync<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. |
FromAsync<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. |
FromAsync<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. |
FromAsync<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. |
FromAsync<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. |
FromAsync<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. |
GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
StartNew(Func<Object,TResult>, Object) |
创建并启动 任务。Creates and starts a task. |
StartNew(Func<Object,TResult>, Object, CancellationToken) |
创建并启动 任务。Creates and starts a task. |
StartNew(Func<Object,TResult>, Object, CancellationToken, TaskCreationOptions, TaskScheduler) |
创建并启动 任务。Creates and starts a task. |
StartNew(Func<Object,TResult>, Object, TaskCreationOptions) |
创建并启动 任务。Creates and starts a task. |
StartNew(Func<TResult>) |
创建并启动 任务。Creates and starts a task. |
StartNew(Func<TResult>, CancellationToken) |
创建并启动 任务。Creates and starts a task. |
StartNew(Func<TResult>, CancellationToken, TaskCreationOptions, TaskScheduler) |
创建并启动 任务。Creates and starts a task. |
StartNew(Func<TResult>, TaskCreationOptions) |
创建并启动 任务。Creates and starts a task. |
ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
适用于
线程安全性
的所有公共和受保护的成员 TaskFactory<TResult> 都是线程安全的,可从多个线程并发使用。All public and protected members of TaskFactory<TResult> are thread-safe and may be used concurrently from multiple threads.