Task.Factory 屬性


提供 Factory 方法的存取,這些 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


Factory 物件,可以建立各種不同的 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:

屬性Property Value
TaskFactory.CancellationToken CancellationToken.None
TaskFactory.ContinuationOptions TaskContinuationOptions.None
TaskFactory.CreationOptions TaskCreationOptions.None
TaskFactory.Scheduler nullTaskScheduler.Currentnull, or TaskScheduler.Current

此屬性最常見的用法是在 TaskFactory.StartNew 方法的單一呼叫中,建立並啟動新的工作。The 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.5開始,Task.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.StartNew 方法進行兩次呼叫。The 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