TaskExtensions.Unwrap Yöntem

Tanım

Aşırı Yüklemeler

Unwrap(Task<Task>)

(C#) veya Task (Of Task) (Visual Basic) zaman uyumsuz işlemini temsil eden bir Task<Task> proxy Task oluşturur.

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

(C#) veya Task (Of Task(Of T)) (Visual Basic) zaman uyumsuz işlemini temsil eden bir Task<Task<T>> proxy Task oluşturur.

Unwrap(Task<Task>)

Source:
TaskExtensions.cs
Source:
TaskExtensions.cs
Source:
TaskExtensions.cs

(C#) veya Task (Of Task) (Visual Basic) zaman uyumsuz işlemini temsil eden bir Task<Task> proxy Task oluşturur.

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

Parametreler

task
Task<Task>

Sarmayı Task<Task> açmak için (C#) veya Task (Of Task) (Visual Basic).

Döndürülenler

Sağlanan System.Threading.Tasks.Task(Of Task)öğesinin zaman uyumsuz işlemini temsil eden görev.

Özel durumlar

Bağımsız değişken null olduğunda task oluşan özel durum.

Örnekler

Aşağıdaki örnekte bir görevin nasıl çıkarılma adımları gösterilmektedir:

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

Açıklamalar

İç Görevin dış Task<TResult>öğesinin bir parçası olarak yapılan işi temsil ettiği bir Task<TResult>içinden Bir Görev döndürebilmek genellikle yararlıdır. Ancak bunun yapılması, dikkatli bir şekilde ele alınmazsa beklenmeyen davranışlara neden olabilecek bir Task<Task> (C#) veya Task (Of Task) (Visual Basic) ile sonuçlanabilir. Unwrap, böyle bir görevin zaman uyumsuz işleminin tamamını temsil eden bir proxy Görevi oluşturarak bu sorunu çözer.

Ayrıca bkz.

Şunlara uygulanır

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

Source:
TaskExtensions.cs
Source:
TaskExtensions.cs
Source:
TaskExtensions.cs

(C#) veya Task (Of Task(Of T)) (Visual Basic) zaman uyumsuz işlemini temsil eden bir Task<Task<T>> proxy Task oluşturur.

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)

Tür Parametreleri

TResult

Görevin sonucunun türü.

Parametreler

task
Task<Task<TResult>>

Sarmayı Task<Task<T>> açmak için (C#) veya Task (Of Task(Of T)) (Visual Basic).

Döndürülenler

Task Sağlanan Task<Task<T>> (C#) veya Task (Of Task(Of T)) (Visual Basic) zaman uyumsuz işlemini temsil eden bir.

Özel durumlar

Bağımsız değişken null olduğunda task oluşan özel durum.

Açıklamalar

genellikle iç öğesinin dış Tasköğesinin Task bir parçası olarak yapılan işi temsil ettiği bir Taskiçinden döndürebilmek Task yararlıdır. Ancak bunun yapılması, dikkatli bir şekilde ele alınmazsa beklenmeyen davranışlara neden olabilecek bir Task<Task<T>> (C#) veya Task (Of Task(Of T)) (Visual Basic) ile sonuçlanabilir. Unwrap, (C#) veya Task (Of Task(Of T)) (Visual Basic) gibi zaman uyumsuz işlemlerin tamamını temsil eden bir Task<Task<T>> ara sunucu Task<TResult> oluşturarak bu sorunu çözer.

Ayrıca bkz.

Şunlara uygulanır