Task<TResult>.Factory 属性

定义

获取用于创建和配置 Task<TResult> 实例的工厂方法。

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)

属性值

一个工厂对象,可创建多种 Task<TResult> 对象。

注解

此属性返回 类的默认实例,该实例 TaskFactory<TResult> 与通过调用无 TaskFactory<TResult>.TaskFactory<TResult>() 参数构造函数创建的实例相同。 它具有以下属性值:

Property “值”
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

适用于

另请参阅