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, 以及、 IsCompletedIsFaulted属性, 用于确定任务的状态。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. 属性返回一个TaskFactory对象, Task<TResult>.Factory属性返回一个TaskFactory<TResult>对象。 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

提供对用于创建和配置 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,同时释放其所有非托管资源。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> 的等待者。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 的浅表副本。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

获取可用于等待任务完成的 WaitHandleGets 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)

无限期等待完成基础 DispatcherOperationWaits 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.

另请参阅