Task.Factory 属性


提供对用于创建和配置 TaskTask<TResult> 实例的工厂方法的访问。Provides access to factory methods for creating and configuring Task and Task<TResult> instances.

 static property System::Threading::Tasks::TaskFactory ^ Factory { System::Threading::Tasks::TaskFactory ^ get(); };
public static System.Threading.Tasks.TaskFactory Factory { get; }
member this.Factory : System.Threading.Tasks.TaskFactory
Public Shared ReadOnly Property Factory As TaskFactory



一个工厂对象,可创建多种 TaskTask<TResult> 对象。A factory object that can create a variety of Task and Task<TResult> objects.


此属性返回类的默认实例,该实例与 TaskFactory 通过调用无参数构造函数创建的实例相同 TaskFactory.TaskFactory()This property returns a default instance of the TaskFactory class that is identical to the one created by calling the parameterless TaskFactory.TaskFactory() constructor. 它具有以下属性值:It has the following property values:

PropertyProperty “值”Value
TaskFactory.CancellationToken CancellationToken.None
TaskFactory.ContinuationOptions TaskContinuationOptions.None
TaskFactory.CreationOptions TaskCreationOptions.None
TaskFactory.Scheduler null、或 TaskScheduler.Currentnull, or TaskScheduler.Current

此属性的最常见用途是在一次调用方法时创建并启动一个新任务 TaskFactory.StartNewThe most common use of this property is to create and start a new task in a single call to the TaskFactory.StartNew method.


从开始 .NET Framework 4.5.NET Framework 4.5Task.Run 方法提供了 Task 使用默认配置值创建对象的最简单方法。Starting with the .NET Framework 4.5.NET Framework 4.5, the Task.Run method provides the easiest way to create a Task object with default configuration values.

下面的示例使用静态 Factory 属性对方法进行两次调用 TaskFactory.StartNewThe following example uses the static Factory property to make two calls to the TaskFactory.StartNew method. 第一种方式是使用用户的 MyDocuments 目录中的文件名称填充数组,而第二次使用用户的 MyDocuments 目录的子目录名称填充数组。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. 然后,它调用 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 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
Imports System.IO
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim tasks(1) As Task
      Dim files() As String = Nothing
      Dim dirs() As String = Nothing
      Dim docsDirectory As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
      tasks(0) = Task.Factory.StartNew( Sub()
                                           files = Directory.GetFiles(docsDirectory)
                                        End Sub )
      tasks(1) = Task.Factory.StartNew( Sub()
                                           dirs = Directory.GetDirectories(docsDirectory)
                                        End Sub )
      Task.Factory.ContinueWhenAll(tasks, Sub(completedTasks)
                                             Console.WriteLine("{0} contains: ", docsDirectory)
                                             Console.WriteLine("   {0} subdirectories", dirs.Length)
                                             Console.WriteLine("   {0} files", files.Length)
                                          End Sub)
   End Sub
End Module
' The example displays output like the following:
'       C:\Users\<username>\Documents contains:
'          24 subdirectories
'          16 files