共用方式為


Task<TResult>.Factory 屬性

定義

取得用於建立和設定 Task<TResult> 實例的 Factory 方法。

public:
 static property System::Threading::Tasks::TaskFactory<TResult> ^ Factory { System::Threading::Tasks::TaskFactory<TResult> ^ get(); };
public static System.Threading.Tasks.TaskFactory<TResult> Factory { get; }
static member Factory : System.Threading.Tasks.TaskFactory<'Result>
Public Shared ReadOnly Property Factory As TaskFactory(Of TResult)

屬性值

Factory 物件,可以建立各種不同的 Task<TResult> 物件。

備註

這個屬性會傳回類別的預設實例,這個實例 TaskFactory<TResult> 與呼叫無 TaskFactory<TResult>.TaskFactory<TResult>() 參數建構函式所建立的實例相同。 其具有下列屬性值:

屬性
TaskFactory<TResult>.CancellationToken CancellationToken.None
TaskFactory<TResult>.ContinuationOptions TaskContinuationOptions.None
TaskFactory<TResult>.CreationOptions TaskCreationOptions.None
TaskFactory<TResult>.Scheduler null,或 TaskScheduler.Current

這個屬性最常見的用法是,在方法的單一呼叫 TaskFactory<TResult>.StartNew 中建立和啟動新工作。

注意

從 .NET Framework 4.5 開始, Task.Run 此方法提供最簡單的方式,以使用預設組態值建立 Task<TResult> 物件。

下列範例會使用靜態 Factory 屬性對 方法進行三次呼叫 TaskFactory<TResult>.StartNew 。 第一個 Task<Int32> 會啟動 物件,它會執行會傳回 1 的 Lambda 運算式。 第二個 Task<Test> 會啟動 物件,它會執行具現化新 Test 實例的 Lambda 運算式。 第三個 Task<String[]> 會啟動 物件,它會列舉 C:\Users\Public\Pictures\Sample Pictures\ 目錄中的檔案。 (請注意,成功執行範例會要求目錄存在且包含檔案。

using System;
using System.Linq;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        // Return a value type with a lambda expression
        Task<int> task1 = Task<int>.Factory.StartNew(() => 1);
        int i = task1.Result;

        // Return a named reference type with a multi-line statement lambda.
        Task<Test> task2 = Task<Test>.Factory.StartNew(() =>
        {
            string s = ".NET";
            double d = 4.0;
            return new Test { Name = s, Number = d };
        });
        Test test = task2.Result;

        // Return an array produced by a PLINQ query
        Task<string[]> task3 = Task<string[]>.Factory.StartNew(() =>
        {
            string path = @"C:\Users\Public\Pictures\Sample Pictures\";
            string[] files = System.IO.Directory.GetFiles(path);

            var result = (from file in files.AsParallel()
                          let info = new System.IO.FileInfo(file)
                          where info.Extension == ".jpg"
                          select file).ToArray();

            return result;
        });

        foreach (var name in task3.Result)
            Console.WriteLine(name);
    }
    class Test
    {
        public string Name { get; set; }
        public double Number { get; set; }
    }
}
Imports System.Threading.Tasks

Module Module1

    Sub Main()
        ReturnAValue()

        Console.WriteLine("Press any key to exit.")
        Console.ReadKey()

    End Sub

    Sub ReturnAValue()

        ' Return a value type with a lambda expression
        Dim task1 = Task(Of Integer).Factory.StartNew(Function() 1)
        Dim i As Integer = task1.Result

        ' Return a named reference type with a multi-line statement lambda.
        Dim task2 As Task(Of Test) = Task.Factory.StartNew(Function()
                                                               Dim s As String = ".NET"
                                                               Dim d As Integer = 4
                                                               Return New Test With {.Name = s, .Number = d}
                                                           End Function)

        Dim myTest As Test = task2.Result
        Console.WriteLine(myTest.Name & ": " & myTest.Number)

        ' Return an array produced by a PLINQ query.
        Dim task3 As Task(Of String())= Task(Of String()).Factory.StartNew(Function()

                                                           Dim path = "C:\Users\Public\Pictures\Sample Pictures\"
                                                           Dim files = System.IO.Directory.GetFiles(path)

                                                           Dim result = (From file In files.AsParallel()
                                                                Let info = New System.IO.FileInfo(file)
                                                                Where info.Extension = ".jpg"
                                                                Select file).ToArray()
                                                           Return result
                                                       End Function)

        For Each name As String In task3.Result
            Console.WriteLine(name)
        Next
    End Sub

    Class Test
        Public Name As String
        Public Number As Double
    End Class
End Module

適用於

另請參閱