TaskExtensions.Unwrap 메서드

정의

오버로드

Unwrap(Task<Task>)

Task의 비동기 작업을 나타내는 프록시 TryExecuteTaskInline(Task, Boolean)를 만듭니다.Creates a proxy Task that represents the asynchronous operation of a TryExecuteTaskInline(Task, Boolean).

Unwrap<TResult>(Task<Task<TResult>>)

Task<Task<T>>(C#) 또는 Task (Of Task(Of T))(Visual Basic)의 비동기 작업을 나타내는 프록시 Task를 만듭니다.Creates a proxy Task that represents the asynchronous operation of a Task<Task<T>> (C#) or Task (Of Task(Of T)) (Visual Basic).

Unwrap(Task<Task>)

Task의 비동기 작업을 나타내는 프록시 TryExecuteTaskInline(Task, Boolean)를 만듭니다.Creates a proxy Task that represents the asynchronous operation of a TryExecuteTaskInline(Task, Boolean).

public:
[System::Runtime::CompilerServices::Extension]
 static System::Threading::Tasks::Task ^ Unwrap(System::Threading::Tasks::Task<System::Threading::Tasks::Task ^> ^ task);
public static System.Threading.Tasks.Task Unwrap (this System.Threading.Tasks.Task<System.Threading.Tasks.Task> task);
static member Unwrap : System.Threading.Tasks.Task<System.Threading.Tasks.Task> -> System.Threading.Tasks.Task
<Extension()>
Public Function Unwrap (task As Task(Of Task)) As Task

매개 변수

task
Task<Task>

래핑을 해제할 Task<Task>(C#) 또는 Task (Of Task)(Visual Basic)입니다.The Task<Task> (C#) or Task (Of Task) (Visual Basic) to unwrap.

반환

Task

제공된 System.Threading.Tasks.Task(Of Task)의 비동기 작업을 나타내는 작업입니다.A Task that represents the asynchronous operation of the provided System.Threading.Tasks.Task(Of Task).

예외

task 인수가 null인 경우 throw되는 예외입니다.The exception that is thrown if the task argument is null.

예제

다음 예제에서는 작업을 래핑 해제 하는 방법을 보여 줍니다.The following example shows how to unwrap a task:

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

class UnWrapDemo
{
     // Demonstrated features:
        //		Task.Unwrap()
        // 		Task.Factory.StartNew()
        //		Task.ContinueWith()
        // Expected results:
        // 		Indicates that continuation chains can be set up virtually instantaneously using Unwrap(), and then left to run on their own.
        //      The results of the RemoteIncrement(0) chain and the RemoteIncrement(4) chain may be intermixed with each other.
        //		The results of the sequence that starts with RemoteIncrement(4) are in strict order.
        // Documentation:
        //		http://msdn.microsoft.com/library/dd781129(VS.100).aspx
        // More information:
        //		http://blogs.msdn.com/pfxteam/archive/2009/11/04/9917581.aspx
        // Other notes:
        //		The combination of Task<T>, ContinueWith() and Unwrap() can be particularly useful for setting up a chain of long-running
        //      tasks where each task uses the results of its predecessor.
        static void Main()
        {
            // Invoking individual tasks is straightforward
            Task<int> t1 = RemoteIncrement(0);
            Console.WriteLine("Started RemoteIncrement(0)");

            // Chain together the results of (simulated) remote operations.
            // The use of Unwrap() instead of .Result below prevents this thread from blocking while setting up this continuation chain.
            Task<int> t2 = RemoteIncrement(4)
                .ContinueWith(t => RemoteIncrement(t.Result))			// RemoteIncrement() returns Task<int> so no unwrapping is needed for the first continuation.
                .Unwrap().ContinueWith(t => RemoteIncrement(t.Result))	// ContinueWith() returns Task<Task<int>>. Therefore unwrapping is needed.
                .Unwrap().ContinueWith(t => RemoteIncrement(t.Result))	// and on it goes...
                .Unwrap();
            Console.WriteLine("Started RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...)");

            try
            {
                t1.Wait();
                Console.WriteLine("Finished RemoteIncrement(0)\n");

                t2.Wait();
                Console.WriteLine("Finished RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...)");
            }
            catch (AggregateException e)
            {
                Console.WriteLine("A task has thrown the following (unexpected) exception:\n{0}", e);
            }
        }
        // This method represents a remote API.
        static Task<int> RemoteIncrement(int n)
        {
            return Task<int>.Factory.StartNew(
                (obj) =>
                {
                    // Simulate a slow operation
                    Thread.Sleep(1 * 1000);

                    int x = (int)obj;
                    Console.WriteLine("Thread={0}, Next={1}", Thread.CurrentThread.ManagedThreadId, ++x);
                    return x;
                },
                n);
        }
}

Imports System.Threading
Imports System.Threading.Tasks

Module UnwrapDemo
    ' Demonstrated features:
    '   Task.Unwrap()
    '    Task.Factory.StartNew()
    '   Task.ContinueWith()
    ' Expected results:
    ' 	Indicates that continuation chains can be set up virtually instantaneously using Unwrap(), and then left to run on their own.
    '   The results of the RemoteIncrement(0) chain and the RemoteIncrement(4) chain may be intermixed with each other.
    '   The results of the sequence that starts with RemoteIncrement(4) are in strict order.
    ' Documentation:
    '   http://msdn.microsoft.com/library/dd781129(VS.100).aspx
    ' More information:
    '   http://blogs.msdn.com/pfxteam/archive/2009/11/04/9917581.aspx
    ' Other notes:
    '   The combination of Task<T>, ContinueWith() and Unwrap() can be particularly useful for setting up a chain of long-running
    '   tasks where each task uses the results of its predecessor.

    Sub Main()
        ' Invoking individual tasks is straightforward
        Dim t1 As Task(Of Integer) = RemoteIncrement(0)
        Console.WriteLine("Started RemoteIncrement(0)")

        ' Chain together the results of (simulated) remote operations.
        ' The use of Unwrap() instead of .Result below prevents this thread from blocking while setting up this continuation chain.
        ' RemoteIncrement() returns Task<int> so no unwrapping is needed for the first continuation.
        ' ContinueWith() here returns Task<Task<int>>. Therefore unwrapping is needed.
        ' and on it goes...
        Dim t2 As Task(Of Integer) = RemoteIncrement(4).ContinueWith(Function(t) RemoteIncrement(t.Result)).Unwrap().ContinueWith(Function(t) RemoteIncrement(t.Result)).Unwrap().ContinueWith(Function(t) RemoteIncrement(t.Result)).Unwrap()
        Console.WriteLine("Started RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...)")

        Try
            t1.Wait()
            Console.WriteLine("Finished RemoteIncrement(0)" & vbLf)

            t2.Wait()
            Console.WriteLine("Finished RemoteIncrement(...(RemoteIncrement(RemoteIncrement(4))...)")
        Catch e As AggregateException
            Console.WriteLine("A task has thrown the following (unexpected) exception:" & vbLf & "{0}", e)

        End Try
    End Sub

    ' This method represents a remote API.
    Function RemoteIncrement(ByVal n As Integer) As Task(Of Integer)
        Return Task(Of Integer).Factory.StartNew(Function(obj)
                                                     ' Simulate a slow operation
                                                     Thread.Sleep(1 * 1000)

                                                     Dim x As Integer = CInt(obj)
                                                     Console.WriteLine("Thread={0}, Next={1}", Thread.CurrentThread.ManagedThreadId, System.Threading.Interlocked.Increment(x))
                                                     Return x
                                                 End Function, n)
    End Function


End Module

설명

에서 작업을 반환할 수 있는 것이 유용한 경우가 많습니다 Task<TResult> . 내부 작업은 외부의 일부로 수행 된 작업을 나타냅니다 Task<TResult> .It is often useful to be able to return a Task from a Task<TResult>, where the inner Task represents work done as part of the outer Task<TResult>. 그러나 이렇게 하면 Task<Task> (c #) 또는 Task (Of Task) (Visual Basic)이 발생 합니다 .이 경우 신중 하 게 처리 하지 않으면 예기치 않은 동작이 발생할 수 있습니다.However, doing so results in a Task<Task> (C#) or Task (Of Task) (Visual Basic), which, if not dealt with carefully, could produce unexpected behavior. 래핑 해제는 이러한 작업의 전체 비동기 작업을 나타내는 프록시 작업을 만들어이 문제를 해결 합니다.Unwrap solves this problem by creating a proxy Task that represents the entire asynchronous operation of such a task.

적용 대상

Unwrap<TResult>(Task<Task<TResult>>)

Task<Task<T>>(C#) 또는 Task (Of Task(Of T))(Visual Basic)의 비동기 작업을 나타내는 프록시 Task를 만듭니다.Creates a proxy Task that represents the asynchronous operation of a Task<Task<T>> (C#) or Task (Of Task(Of T)) (Visual Basic).

public:
generic <typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Threading::Tasks::Task<TResult> ^ Unwrap(System::Threading::Tasks::Task<System::Threading::Tasks::Task<TResult> ^> ^ task);
public static System.Threading.Tasks.Task<TResult> Unwrap<TResult> (this System.Threading.Tasks.Task<System.Threading.Tasks.Task<TResult>> task);
static member Unwrap : System.Threading.Tasks.Task<System.Threading.Tasks.Task<'Result>> -> System.Threading.Tasks.Task<'Result>
<Extension()>
Public Function Unwrap(Of TResult) (task As Task(Of Task(Of TResult))) As Task(Of TResult)

형식 매개 변수

TResult

작업 결과의 형식입니다.The type of the task's result.

매개 변수

task
Task<Task<TResult>>

래핑을 해제할 Task<Task<T>>(C#) 또는 Task (Of Task(Of T))(Visual Basic)입니다.The Task<Task<T>> (C#) or Task (Of Task(Of T)) (Visual Basic) to unwrap.

반환

Task<TResult>

제공된 Task<Task<T>>(C#) 또는 Task (Of Task(Of T))(Visual Basic)의 비동기 작업을 나타내는 Task입니다.A Task that represents the asynchronous operation of the provided Task<Task<T>> (C#) or Task (Of Task(Of T)) (Visual Basic).

예외

task 인수가 null인 경우 throw되는 예외입니다.The exception that is thrown if the task argument is null.

설명

에서를 반환 하는 것이 유용한 경우가 종종 Task Task 있습니다. 여기서 inner는 Task 외부의 일부로 수행 되는 작업을 나타냅니다 Task .It is often useful to be able to return a Task from a Task, where the inner Task represents work done as part of the outer Task. 그러나 이렇게 하면 Task<Task<T>> (c #) 또는 Task (Of Task(Of T)) (Visual Basic)이 발생 합니다 .이 경우 신중 하 게 처리 하지 않으면 예기치 않은 동작이 발생할 수 있습니다.However, doing so results in a Task<Task<T>> (C#) or Task (Of Task(Of T)) (Visual Basic), which, if not dealt with carefully, could produce unexpected behavior. 래핑 해제 Task<TResult> 는 이러한 Task<Task<T>> (c #) 또는 (Visual Basic)의 전체 비동기 작업을 나타내는 프록시를 만들어이 문제를 해결 Task (Of Task(Of T)) 합니다.Unwrap solves this problem by creating a proxy Task<TResult> that represents the entire asynchronous operation of such a Task<Task<T>> (C#) or Task (Of Task(Of T)) (Visual Basic).

적용 대상