Task<TResult> 類別

定義

表示可傳回值的非同步作業。Represents an asynchronous operation that can return a value.

generic <typename TResult>
public ref class Task : System::Threading::Tasks::Task
public class Task<TResult> : System.Threading.Tasks.Task
type Task<'Result> = class
    inherit Task
Public Class Task(Of TResult)
Inherits Task

類型參數

TResult

這個 Task<TResult> 所產生之結果的型別。The type of the result produced by this Task<TResult>.

繼承
Task<TResult>

備註

Task<TResult>類別代表傳回值且通常以非同步方式執行的單一作業。The Task<TResult> class represents a single operation that returns a value and that usually executes asynchronously. Task<TResult>物件是以工作為基礎之非同步模式的其中一個中央元件, 第一次是在 .NET Framework 4 中引進。Task<TResult> objects are one of the central components of the task-based asynchronous pattern first introduced in the .NET Framework 4. 由於Task<TResult>物件所執行的工作通常會線上程集區執行緒上以非同步方式執行, 而不是在主應用程式執行緒上Status同步執行, 因此您可以IsCanceled使用IsCompleted屬性, 以及、和。IsFaulted屬性, 用來判斷工作的狀態。Because the work performed by a Task<TResult> object typically executes asynchronously on a thread pool thread rather than synchronously on the main application thread, you can use the Status property, as well as the IsCanceled, IsCompleted, and IsFaulted properties, to determine the state of a task. 最常見的情況是使用 lambda 運算式來指定工作要執行的工作。Most commonly, a lambda expression is used to specify the work that the task is to perform.

Task<TResult>實例可能會以各種不同的方式建立。Task<TResult> instances may be created in a variety of ways. 從開始.NET Framework 4.5.NET Framework 4.5提供最常見的方法是呼叫靜態Task.Run<TResult>(Func<TResult>)Task.Run<TResult>(Func<TResult>, CancellationToken)方法。The most common approach, which is available starting with the .NET Framework 4.5.NET Framework 4.5, is to call the static Task.Run<TResult>(Func<TResult>) or Task.Run<TResult>(Func<TResult>, CancellationToken) method. 這些方法提供簡單的方法, 讓您使用預設值來啟動工作, 而不需要取得其他參數。These methods provide a simple way to start a task by using default values and without acquiring additional parameters. 下列範例會使用Task.Run<TResult>(Func<TResult>)方法來啟動迴圈的工作, 然後顯示迴圈反覆運算的數目:The following example uses the Task.Run<TResult>(Func<TResult>) method to start a task that loops and then displays the number of loop iterations:

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      var t = Task<int>.Run( () => {
                                      // Just loop.
                                      int max = 1000000;
                                      int ctr = 0;
                                      for (ctr = 0; ctr <= max; ctr++) {
                                         if (ctr == max / 2 && DateTime.Now.Hour <= 12) {
                                            ctr++;
                                            break;
                                         }
                                      }
                                      return ctr;
                                    } );
      Console.WriteLine("Finished {0:N0} iterations.", t.Result);
   }
}
// The example displays output like the following:
//        Finished 1,000,001 loop iterations.
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim t As Task(Of Integer) = Task.Run(Function()
                                  Dim max As Integer = 1000000
                                  Dim ctr As Integer
                                  For ctr = 0 to max
                                     If ctr = max \ 2 And Date.Now.Hour <= 12 Then
                                        ctr += 1
                                        Exit For
                                     End If
                                  Next
                                  Return ctr
                               End Function)
      Console.WriteLine("Finished {0:N0} iterations.", t.Result)
   End Sub
End Module
' The example displays the following output:
'       Finished 1,000,001 loop iterations

另一個替代方法是在中.NET Framework 4.NET Framework 4啟動工作的最常見方式, 就是呼叫靜態TaskFactory.StartNewTaskFactory<TResult>.StartNew方法。An alternative, and the most common way to start a task in the .NET Framework 4.NET Framework 4, is to call the static TaskFactory.StartNew or TaskFactory<TResult>.StartNew method. 屬性會傳回Task<TResult>.Factory 物件,TaskFactory<TResult>而屬性會傳回物件。 TaskFactory Task.FactoryThe Task.Factory property returns a TaskFactory object, and the Task<TResult>.Factory property returns a TaskFactory<TResult> object. StartNew方法的多載可讓您傳遞引數、定義工作建立選項, 以及指定工作排程器。Overloads of their StartNew method let you pass arguments, define task creation options, and specify a task scheduler. 下列範例會使用TaskFactory<TResult>.StartNew(Func<TResult>)方法來啟動工作。The following example uses the TaskFactory<TResult>.StartNew(Func<TResult>) method to start a task. 它在功能上相當於前一個範例中的程式碼。It is functionally equivalent to the code in the previous example.

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      var t = Task<int>.Factory.StartNew( () => {
                                      // Just loop.
                                      int max = 1000000;
                                      int ctr = 0;
                                      for (ctr = 0; ctr <= max; ctr++) {
                                         if (ctr == max / 2 && DateTime.Now.Hour <= 12) {
                                            ctr++;
                                            break;
                                         }
                                      }
                                      return ctr;
                               } );
      Console.WriteLine("Finished {0:N0} iterations.", t.Result);
   }
}
// The example displays the following output:
//        Finished 1000001 loop iterations
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim t = Task(Of Integer).Factory.StartNew(Function()
                                  Dim max As Integer = 1000000
                                  Dim ctr As Integer
                                  For ctr = 0 to max
                                     If ctr = max \ 2 And Date.Now.Hour <= 12 Then
                                       ctr += 1
                                       Exit For
                                     End If
                                  Next
                                  Return ctr
                               End Function)
      Console.WriteLine("Finished {0:N0} iterations.", t.Result)
   End Sub
End Module
' The example displays output like the following:
'       Finished 1,000,001 iterations

如需更完整的範例, 請參閱以工作為基礎的非同步程式設計For more complete examples, see Task-based Asynchronous Programming.

Task<TResult>類別也提供初始化工作的函式, 但不會排程執行。The Task<TResult> class also provides constructors that initialize the task but that do not schedule it for execution. 基於效能的考慮, Task.RunTask.Factory.StartNew方法是建立和排程計算工作的慣用機制, 但對於必須分隔工作建立和排程的案例, 可以使用此函式, 以及工作的Start方法可以接著用來排程工作, 以便在稍後執行。For performance reasons, the Task.Run and Task.Factory.StartNew methods are the preferred mechanisms for creating and scheduling computational tasks, but for scenarios where task creation and scheduling must be separated, the constructors may be used, and the task's Start method may then be used to schedule the task for execution at a later time.

從以為目標的.NET Framework 4.6.NET Framework 4.6桌面應用程式開始, 建立和叫用工作之執行緒的文化特性會成為執行緒內容的一部分。Starting with desktop apps that target the .NET Framework 4.6.NET Framework 4.6, the culture of the thread that creates and invokes a task becomes part of the thread's context. 也就是說, 不論工作執行所在之執行緒的目前文化特性為何, 工作的目前文化特性都是呼叫執行緒的文化特性。That is, regardless of the current culture of the thread on which the task executes, the current culture of the task is the culture of the calling thread. 對於以.NET Framework 4.6.NET Framework 4.6之前的 .NET Framework 版本為目標的應用程式, 工作的文化特性就是工作執行所在之執行緒的文化特性。For apps that target versions of the .NET Framework prior to the .NET Framework 4.6.NET Framework 4.6, the culture of the task is the culture of the thread on which the task executes. 如需詳細資訊, 請參閱CultureInfo主題中的「文化特性和以工作為基礎的非同步作業」一節。For more information, see the "Culture and task-based asynchronous operations" section in the CultureInfo topic. 請注意, 存放區應用程式會遵循設定和取得預設文化特性中的 Windows 執行階段。Note that Store apps follow the Windows Runtime in setting and getting the default culture.

對於不會傳回值的作業, 您可以使用Task類別。For operations that do not return a value, you use the Task class. 從C# 7.0 開始, 針對屬於實值型別而不是參考型別的較輕量工作, 請System.Threading.Tasks.ValueTask<TResult>使用結構。Starting with C# 7.0, for a more lightweight task that is a value type rather than a reference type, use the System.Threading.Tasks.ValueTask<TResult> structure.

建構函式

Task<TResult>(Func<Object,TResult>, Object)

使用指定的函式和狀態,初始化新的 Task<TResult>Initializes a new Task<TResult> with the specified function and state.

Task<TResult>(Func<Object,TResult>, Object, CancellationToken)

使用指定的動作、狀態和選項,初始化新的 Task<TResult>Initializes a new Task<TResult> with the specified action, state, and options.

Task<TResult>(Func<Object,TResult>, Object, CancellationToken, TaskCreationOptions)

使用指定的動作、狀態和選項,初始化新的 Task<TResult>Initializes a new Task<TResult> with the specified action, state, and options.

Task<TResult>(Func<Object,TResult>, Object, TaskCreationOptions)

使用指定的動作、狀態和選項,初始化新的 Task<TResult>Initializes a new Task<TResult> with the specified action, state, and options.

Task<TResult>(Func<TResult>)

使用指定的函式,初始化新的 Task<TResult>Initializes a new Task<TResult> with the specified function.

Task<TResult>(Func<TResult>, CancellationToken)

使用指定的函式,初始化新的 Task<TResult>Initializes a new Task<TResult> with the specified function.

Task<TResult>(Func<TResult>, CancellationToken, TaskCreationOptions)

使用指定的函式和建立選項,初始化新的 Task<TResult>Initializes a new Task<TResult> with the specified function and creation options.

Task<TResult>(Func<TResult>, TaskCreationOptions)

使用指定的函式和建立選項,初始化新的 Task<TResult>Initializes a new Task<TResult> with the specified function and creation options.

屬性

AsyncState

取得建立 Task 時提供的狀態物件,如果未提供則為 null。Gets the state object supplied when the Task was created, or null if none was supplied.

(繼承來源 Task)
CreationOptions

取得用來建立這個工作的 TaskCreationOptionsGets the TaskCreationOptions used to create this task.

(繼承來源 Task)
Exception

取得導致 AggregateException 不當結束的 TaskGets the AggregateException that caused the Task to end prematurely. 如果 Task 順利完成,或未擲回任何例外狀況,則這會傳回 nullIf the Task completed successfully or has not yet thrown any exceptions, this will return null.

(繼承來源 Task)
Factory

提供 Factory 方法的存取,這些 Factory 方法用於建立及設定 Task<TResult> 執行個體。Provides access to factory methods for creating and configuring Task<TResult> instances.

Id

取得這個 Task 執行個體的 ID。Gets an ID for this Task instance.

(繼承來源 Task)
IsCanceled

取得這個 Task 執行個體是否因取消才完成執行。Gets whether this Task instance has completed execution due to being canceled.

(繼承來源 Task)
IsCompleted

取得值,這個值表示工作是否已經完成。Gets a value that indicates whether the task has completed.

(繼承來源 Task)
IsCompletedSuccessfully

取得工作是否執行到完成。Gets whether the task ran to completion.

(繼承來源 Task)
IsFaulted

取得 Task 是否因未處理的例外狀況才完成。Gets whether the Task completed due to an unhandled exception.

(繼承來源 Task)
Result

取得這個 Task<TResult> 的結果值。Gets the result value of this Task<TResult>.

Status

取得這個工作的 TaskStatusGets the TaskStatus of this task.

(繼承來源 Task)

方法

ConfigureAwait(Boolean)

設定用來等候這個 Task<TResult> 的 awaiter。Configures an awaiter used to await this Task<TResult>.

ContinueWith(Action<Task,Object>, Object)

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊並執行的接續。Creates a continuation that receives caller-supplied state information and executes when the target Task completes.

(繼承來源 Task)
ContinueWith(Action<Task,Object>, Object, CancellationToken)

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊和取消語彙基元,並非同步執行的接續。Creates a continuation that receives caller-supplied state information and a cancellation token and that executes asynchronously when the target Task completes.

(繼承來源 Task)
ContinueWith(Action<Task,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊和取消語彙基元,並執行的接續。Creates a continuation that receives caller-supplied state information and a cancellation token and that executes when the target Task completes. 接續會根據一組指定的條件執行,並使用指定的排程器。The continuation executes based on a set of specified conditions and uses a specified scheduler.

(繼承來源 Task)
ContinueWith(Action<Task,Object>, Object, TaskContinuationOptions)

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊並執行的接續。Creates a continuation that receives caller-supplied state information and executes when the target Task completes. 接續會根據一組指定的條件執行。The continuation executes based on a set of specified conditions.

(繼承來源 Task)
ContinueWith(Action<Task,Object>, Object, TaskScheduler)

建立當目標 Task 完成時,會接收呼叫端提供的狀態資訊並非同步執行的接續。Creates a continuation that receives caller-supplied state information and executes asynchronously when the target Task completes. 接續會使用指定的排程器。The continuation uses a specified scheduler.

(繼承來源 Task)
ContinueWith(Action<Task<TResult>,Object>, Object)

建立當目標 Task<TResult> 完成時,會接收狀態資訊並執行的接續。Creates a continuation that is passed state information and that executes when the target Task<TResult> completes.

ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken)

建立目標 Task<TResult> 完成時執行的接續工作。Creates a continuation that executes when the target Task<TResult> completes.

ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

建立目標 Task<TResult> 完成時執行的接續工作。Creates a continuation that executes when the target Task<TResult> completes.

ContinueWith(Action<Task<TResult>,Object>, Object, TaskContinuationOptions)

建立目標 Task<TResult> 完成時執行的接續工作。Creates a continuation that executes when the target Task<TResult> completes.

ContinueWith(Action<Task<TResult>,Object>, Object, TaskScheduler)

建立目標 Task<TResult> 完成時執行的接續工作。Creates a continuation that executes when the target Task<TResult> completes.

ContinueWith(Action<Task<TResult>>)

建立當目標工作完成時非同步執行的接續。Creates a continuation that executes asynchronously when the target task completes.

ContinueWith(Action<Task<TResult>>, CancellationToken)

建立當目標 Task<TResult> 完成時非同步執行的可取消接續。Creates a cancelable continuation that executes asynchronously when the target Task<TResult> completes.

ContinueWith(Action<Task<TResult>>, CancellationToken, TaskContinuationOptions, TaskScheduler)

建立會根據 continuationOptions 中指定的條件執行的接續。Creates a continuation that executes according the condition specified in continuationOptions.

ContinueWith(Action<Task<TResult>>, TaskContinuationOptions)

建立會根據 continuationOptions 中指定的條件執行的接續。Creates a continuation that executes according the condition specified in continuationOptions.

ContinueWith(Action<Task<TResult>>, TaskScheduler)

建立當目標 Task<TResult> 完成時非同步執行的接續。Creates a continuation that executes asynchronously when the target Task<TResult> completes.

ContinueWith(Action<Task>)

建立當目標 Task 完成時非同步執行的接續。Creates a continuation that executes asynchronously when the target Task completes.

(繼承來源 Task)
ContinueWith(Action<Task>, CancellationToken)

建立當目標 Task 完成時,會接收取消語彙基元並非同步執行的接續。Creates a continuation that receives a cancellation token and executes asynchronously when the target Task completes.

(繼承來源 Task)
ContinueWith(Action<Task>, CancellationToken, TaskContinuationOptions, TaskScheduler)

建立當目標工作完成時,會根據指定之 TaskContinuationOptions 執行的接續。Creates a continuation that executes when the target task competes according to the specified TaskContinuationOptions. 接續會接收取消語彙基元,並使用指定的排程器。The continuation receives a cancellation token and uses a specified scheduler.

(繼承來源 Task)
ContinueWith(Action<Task>, TaskContinuationOptions)

建立當目標工作完成時,會根據指定之 TaskContinuationOptions 執行的接續。Creates a continuation that executes when the target task completes according to the specified TaskContinuationOptions.

(繼承來源 Task)
ContinueWith(Action<Task>, TaskScheduler)

建立當目標 Task 完成時非同步執行的接續。Creates a continuation that executes asynchronously when the target Task completes. 接續會使用指定的排程器。The continuation uses a specified scheduler.

(繼承來源 Task)
ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object)

建立目標 Task<TResult> 完成時執行的接續工作。Creates a continuation that executes when the target Task<TResult> completes.

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken)

建立目標 Task<TResult> 完成時執行的接續工作。Creates a continuation that executes when the target Task<TResult> completes.

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

建立目標 Task<TResult> 完成時執行的接續工作。Creates a continuation that executes when the target Task<TResult> completes.

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskContinuationOptions)

建立目標 Task<TResult> 完成時執行的接續工作。Creates a continuation that executes when the target Task<TResult> completes.

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskScheduler)

建立目標 Task<TResult> 完成時執行的接續工作。Creates a continuation that executes when the target Task<TResult> completes.

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>)

建立當目標 Task<TResult> 完成時非同步執行的接續。Creates a continuation that executes asynchronously when the target Task<TResult> completes.

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken)

建立當目標 Task<TResult> 完成時非同步執行的接續。Creates a continuation that executes asynchronously when the target Task<TResult> completes.

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

建立會根據 continuationOptions 中指定的條件執行的接續。Creates a continuation that executes according the condition specified in continuationOptions.

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskContinuationOptions)

建立會根據 continuationOptions 中指定的條件執行的接續。Creates a continuation that executes according the condition specified in continuationOptions.

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskScheduler)

建立當目標 Task<TResult> 完成時非同步執行的接續。Creates a continuation that executes asynchronously when the target Task<TResult> completes.

ContinueWith<TResult>(Func<Task,Object,TResult>, Object)

建立當目標 Task 完成並傳回值時,會接收呼叫端提供的狀態資訊並非同步執行的接續。Creates a continuation that receives caller-supplied state information and executes asynchronously when the target Task completes and returns a value.

(繼承來源 Task)
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken)

建立當目標 Task 完成並傳回值時非同步執行的接續。Creates a continuation that executes asynchronously when the target Task completes and returns a value. 接續會接收呼叫端提供的狀態資訊和取消語彙基元。The continuation receives caller-supplied state information and a cancellation token.

(繼承來源 Task)
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

建立當目標 Task 完成並傳回值時,會根據指定的工作接續選項來執行的接續。Creates a continuation that executes based on the specified task continuation options when the target Task completes and returns a value. 接續會接收呼叫端提供的狀態資訊和取消語彙基元,並使用指定的排程器。The continuation receives caller-supplied state information and a cancellation token and uses the specified scheduler.

(繼承來源 Task)
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskContinuationOptions)

建立當目標 Task 完成時,會根據指定的工作接續選項來執行的接續。Creates a continuation that executes based on the specified task continuation options when the target Task completes. 接續會接收呼叫端提供的狀態資訊。The continuation receives caller-supplied state information.

(繼承來源 Task)
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskScheduler)

建立當目標 Task 完成時非同步執行的接續。Creates a continuation that executes asynchronously when the target Task completes. 接續會接收呼叫端提供的狀態資訊,並使用指定的排程器。The continuation receives caller-supplied state information and uses a specified scheduler.

(繼承來源 Task)
ContinueWith<TResult>(Func<Task,TResult>)

建立當目標 Task<TResult> 完成並傳回值時非同步執行的接續。Creates a continuation that executes asynchronously when the target Task<TResult> completes and returns a value.

(繼承來源 Task)
ContinueWith<TResult>(Func<Task,TResult>, CancellationToken)

建立當目標 Task 完成並傳回值時非同步執行的接續。Creates a continuation that executes asynchronously when the target Task completes and returns a value. 接續會接收取消語彙基元。The continuation receives a cancellation token.

(繼承來源 Task)
ContinueWith<TResult>(Func<Task,TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

建立會根據指定之接續選項執行並傳回值的接續。Creates a continuation that executes according to the specified continuation options and returns a value. 接續會接收取消語彙基元,並使用指定的排程器。The continuation is passed a cancellation token and uses a specified scheduler.

(繼承來源 Task)
ContinueWith<TResult>(Func<Task,TResult>, TaskContinuationOptions)

建立會根據指定之接續選項執行並傳回值的接續。Creates a continuation that executes according to the specified continuation options and returns a value.

(繼承來源 Task)
ContinueWith<TResult>(Func<Task,TResult>, TaskScheduler)

建立當目標 Task 完成並傳回值時非同步執行的接續。Creates a continuation that executes asynchronously when the target Task completes and returns a value. 接續會使用指定的排程器。The continuation uses a specified scheduler.

(繼承來源 Task)
Dispose()

釋放 Task 類別目前的執行個體所使用的全部資源。Releases all resources used by the current instance of the Task class.

(繼承來源 Task)
Dispose(Boolean)

處置 Task,並釋放它的所有 Unmanaged 資源。Disposes the Task, releasing all of its unmanaged resources.

(繼承來源 Task)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetAwaiter()

取得用來等候這個 Task<TResult> 的 awaiter。Gets an awaiter used to await this Task<TResult>.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
RunSynchronously()

在目前的 Task 上同步執行 TaskSchedulerRuns the Task synchronously on the current TaskScheduler.

(繼承來源 Task)
RunSynchronously(TaskScheduler)

在提供的 Task 上同步執行 TaskSchedulerRuns the Task synchronously on the TaskScheduler provided.

(繼承來源 Task)
Start()

啟動 Task,並排定它在目前的 TaskScheduler 執行。Starts the Task, scheduling it for execution to the current TaskScheduler.

(繼承來源 Task)
Start(TaskScheduler)

啟動 Task,並排定它在指定的 TaskScheduler 執行。Starts the Task, scheduling it for execution to the specified TaskScheduler.

(繼承來源 Task)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
Wait()

等候 Task 完成執行。Waits for the Task to complete execution.

(繼承來源 Task)
Wait(CancellationToken)

等候 Task 完成執行。Waits for the Task to complete execution. 如果在工作完成之前取消語彙基元已取消,則等候會終止。The wait terminates if a cancellation token is canceled before the task completes.

(繼承來源 Task)
Wait(Int32)

等待 Task 在指定的毫秒數內完成執行。Waits for the Task to complete execution within a specified number of milliseconds.

(繼承來源 Task)
Wait(Int32, CancellationToken)

等候 Task 完成執行。Waits for the Task to complete execution. 如果在工作完成之前經過逾時間隔或取消語彙基元已取消,則等候會終止。The wait terminates if a timeout interval elapses or a cancellation token is canceled before the task completes.

(繼承來源 Task)
Wait(TimeSpan)

等待 Task 在指定的時間間隔內完成執行。Waits for the Task to complete execution within a specified time interval.

(繼承來源 Task)

明確介面實作

IAsyncResult.AsyncWaitHandle

取得 WaitHandle,這個項目可用來等候工作完成。Gets a WaitHandle that can be used to wait for the task to complete.

(繼承來源 Task)
IAsyncResult.CompletedSynchronously

取得作業是否已同步完成的指示。Gets an indication of whether the operation completed synchronously.

(繼承來源 Task)

擴充方法

DispatcherOperationWait(Task)

無限期等候基礎 DispatcherOperation 完成。Waits indefinitely for the underlying DispatcherOperation to complete.

DispatcherOperationWait(Task, TimeSpan)

在指定的時間長度中等候,等候基礎 DispatcherOperation 完成。Waits for the specified amount of time for the underlying DispatcherOperation to complete.

IsDispatcherOperationTask(Task)

傳回值,表示這個 Task 是否與 DispatcherOperation 關聯。Returns a value that indicates whether this Task is associated with a DispatcherOperation.

AsAsyncAction(Task)

傳回代表已開始的動作的 Windows 執行階段非同步動作。Returns a Windows Runtime asynchronous action that represents a started task.

AsAsyncOperation<TResult>(Task<TResult>)

傳回 Windows 執行階段非同步作業,代表會傳回結果的已開始的工作。Returns a Windows Runtime asynchronous operation that represents a started task that returns a result.

適用於

執行緒安全性

除了以外Task<TResult>,的所有成員都是安全線程, 而且可以同時從多個執行緒使用。 Dispose()All members of Task<TResult>, except for Dispose(), are thread-safe and may be used from multiple threads concurrently.

另請參閱