Task<TResult>.Factory 속성

정의

Task<TResult> 인스턴스를 만들고 구성하는 팩터리 메서드에 대한 액세스를 제공합니다.Provides access to factory methods for creating and configuring Task<TResult> instances.

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

속성 값

다양한 Task<TResult> 개체를 만들 수 있는 팩터리 개체입니다.A factory object that can create a variety of Task<TResult> objects.

설명

이 속성의 기본 인스턴스를 반환 합니다 TaskFactory<TResult> 클래스는 매개 변수가 없는 호출 하 여 만든 동일한 TaskFactory<TResult>.TaskFactory<TResult>() 생성자입니다.This property returns a default instance of the TaskFactory<TResult> class that is identical to the one created by calling the parameterless TaskFactory<TResult>.TaskFactory<TResult>() constructor. 다음 속성 값을 포함 합니다.It has the following property values:

속성Property Value
TaskFactory<TResult>.CancellationToken CancellationToken.None
TaskFactory<TResult>.ContinuationOptions TaskContinuationOptions.None
TaskFactory<TResult>.CreationOptions TaskCreationOptions.None
TaskFactory<TResult>.Scheduler null또는 TaskScheduler.Currentnull, or TaskScheduler.Current

이 속성의 가장 일반적인 용도 만들고 단일 호출에서 새 작업을 시작 하는 것은 TaskFactory<TResult>.StartNew 메서드.The most common use of this property is to create and start a new task in a single call to the TaskFactory<TResult>.StartNew method.

참고

부터 합니다 .NET Framework 4.5.NET Framework 4.5Task.Run 메서드를 만드는 가장 쉬운 방법은 제공을 Task<TResult> 기본 구성 값을 사용 하 여 개체입니다.Starting with the .NET Framework 4.5.NET Framework 4.5, the Task.Run method provides the easiest way to create a Task<TResult> object with default configuration values.

다음 예제에서는 정적 Factory 속성에 대 한 세 개의 호출을는 TaskFactory<TResult>.StartNew 메서드.The following example uses the static Factory property to make three calls to the TaskFactory<TResult>.StartNew method. 첫 번째 시작을 Task<Int32> 1을 반환 하는 람다 식을 실행 하는 개체입니다.The first starts a Task<Int32> object, which executes a lambda expression that returns 1. 두 번째 시작을 Task<Test> 새 인스턴스화하는 람다 식을 실행 하는 개체 Test 인스턴스.The second starts a Task<Test> object, which executes a lambda expression that instantiates a new Test instance. 세 번째는 C:\Users\Public\Pictures\Sample Task<String[]> Pictures \ 디렉터리의 파일을 열거 하는 개체를 시작 합니다.The third starts a Task<String[]> object, which enumerates the files in the C:\Users\Public\Pictures\Sample Pictures\ directory. 예제를 성공적으로 실행 하려면 디렉터리가 존재 하 고 파일이 포함 되어 있어야 합니다.(Note that successful execution of the example requires that the directory exist and that it contain files.

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

적용 대상

추가 정보