Task<TResult> Task<TResult> Task<TResult> Task<TResult> Class

定义

表示一个可以返回值的异步操作。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>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属性,并将IsCanceledIsCompleted,和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.Factory属性返回TaskFactory对象,并Task<TResult>.Factory属性返回TaskFactory<TResult>对象。The 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 的应用, .NET Framework 4.6.NET Framework 4.6,任务的区域性是执行该任务时的线程的区域性。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>(Func<Object,TResult>, Object) Task<TResult>(Func<Object,TResult>, Object) 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>(Func<Object,TResult>, Object, CancellationToken) Task<TResult>(Func<Object,TResult>, Object, CancellationToken) 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>(Func<Object,TResult>, Object, CancellationToken, TaskCreationOptions) Task<TResult>(Func<Object,TResult>, Object, CancellationToken, TaskCreationOptions) 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>(Func<Object,TResult>, Object, TaskCreationOptions) Task<TResult>(Func<Object,TResult>, Object, TaskCreationOptions) 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>(Func<TResult>) Task<TResult>(Func<TResult>) Task<TResult>(Func<TResult>)

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

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

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

Task<TResult>(Func<TResult>, CancellationToken, TaskCreationOptions) Task<TResult>(Func<TResult>, CancellationToken, TaskCreationOptions) Task<TResult>(Func<TResult>, CancellationToken, TaskCreationOptions) 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>(Func<TResult>, TaskCreationOptions) Task<TResult>(Func<TResult>, TaskCreationOptions) Task<TResult>(Func<TResult>, TaskCreationOptions)

使用指定的函数和创建选项初始化新的 Task<TResult>Initializes a new Task<TResult> with the specified function and creation options.

属性

AsyncState AsyncState AsyncState AsyncState

获取在创建 Task 时提供的状态对象,如果未提供,则为 null。Gets the state object supplied when the Task was created, or null if none was supplied.

(Inherited from Task)
CreationOptions CreationOptions CreationOptions CreationOptions

获取用于创建此任务的 TaskCreationOptionsGets the TaskCreationOptions used to create this task.

(Inherited from Task)
Exception Exception Exception 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.

(Inherited from Task)
Factory Factory Factory Factory

提供对用于创建和配置 Task<TResult> 实例的工厂方法的访问。Provides access to factory methods for creating and configuring Task<TResult> instances.

IAsyncResult.AsyncWaitHandle IAsyncResult.AsyncWaitHandle IAsyncResult.AsyncWaitHandle IAsyncResult.AsyncWaitHandle

获取可用于等待任务完成的 WaitHandleGets a WaitHandle that can be used to wait for the task to complete.

(Inherited from Task)
IAsyncResult.CompletedSynchronously IAsyncResult.CompletedSynchronously IAsyncResult.CompletedSynchronously IAsyncResult.CompletedSynchronously

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

(Inherited from Task)
Id Id Id Id

获取此 Task 实例的 ID。Gets an ID for this Task instance.

(Inherited from Task)
IsCanceled IsCanceled IsCanceled IsCanceled

获取此 Task 实例是否由于被取消的原因而已完成执行。Gets whether this Task instance has completed execution due to being canceled.

(Inherited from Task)
IsCompleted IsCompleted IsCompleted IsCompleted

获取一个值,它表示是否已完成任务。Gets a value that indicates whether the task has completed.

(Inherited from Task)
IsCompletedSuccessfully IsCompletedSuccessfully IsCompletedSuccessfully IsCompletedSuccessfully Inherited from Task
IsFaulted IsFaulted IsFaulted IsFaulted

获取 Task 是否由于未经处理异常的原因而完成。Gets whether the Task completed due to an unhandled exception.

(Inherited from Task)
Result Result Result Result

获取此 Task<TResult> 的结果值。Gets the result value of this Task<TResult>.

Status Status Status Status

获取此任务的 TaskStatusGets the TaskStatus of this task.

(Inherited from Task)

方法

ConfigureAwait(Boolean) ConfigureAwait(Boolean) ConfigureAwait(Boolean) ConfigureAwait(Boolean)

配置用于等待此 Task<TResult>的 awaiter。Configures an awaiter used to await this Task<TResult>.

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

创建一个在目标 Task 完成时接收调用方提供的状态信息并执行的延续任务。Creates a continuation that receives caller-supplied state information and executes when the target Task completes.

(Inherited from Task)
ContinueWith(Action<Task,Object>, Object, CancellationToken) ContinueWith(Action<Task,Object>, Object, CancellationToken) ContinueWith(Action<Task,Object>, Object, CancellationToken) 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.

(Inherited from Task)
ContinueWith(Action<Task,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith(Action<Task,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith(Action<Task,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) 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.

(Inherited from Task)
ContinueWith(Action<Task,Object>, Object, TaskContinuationOptions) ContinueWith(Action<Task,Object>, Object, TaskContinuationOptions) ContinueWith(Action<Task,Object>, Object, TaskContinuationOptions) 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.

(Inherited from Task)
ContinueWith(Action<Task,Object>, Object, TaskScheduler) ContinueWith(Action<Task,Object>, Object, TaskScheduler) ContinueWith(Action<Task,Object>, Object, TaskScheduler) 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.

(Inherited from Task)
ContinueWith(Action<Task<TResult>,Object>, Object) ContinueWith(Action<Task<TResult>,Object>, Object) ContinueWith(Action<Task<TResult>,Object>, Object) 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) ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken) ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken) 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) ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) 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) ContinueWith(Action<Task<TResult>,Object>, Object, TaskContinuationOptions) ContinueWith(Action<Task<TResult>,Object>, Object, TaskContinuationOptions) 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) ContinueWith(Action<Task<TResult>,Object>, Object, TaskScheduler) ContinueWith(Action<Task<TResult>,Object>, Object, TaskScheduler) ContinueWith(Action<Task<TResult>,Object>, Object, TaskScheduler)

创建一个在目标 Task<TResult> 完成时执行的延续任务。Creates a continuation that executes when the target Task<TResult> completes.

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

创建一个在目标任务完成时异步执行的延续任务。Creates a continuation that executes asynchronously when the target task completes.

ContinueWith(Action<Task<TResult>>, CancellationToken) ContinueWith(Action<Task<TResult>>, CancellationToken) ContinueWith(Action<Task<TResult>>, CancellationToken) 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) ContinueWith(Action<Task<TResult>>, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith(Action<Task<TResult>>, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith(Action<Task<TResult>>, CancellationToken, TaskContinuationOptions, TaskScheduler)

创建根据 continuationOptions 中指定的条件加以执行的延续任务。Creates a continuation that executes according the condition specified in continuationOptions.

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

创建根据 continuationOptions 中指定的条件加以执行的延续任务。Creates a continuation that executes according the condition specified in continuationOptions.

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

创建一个在目标 Task<TResult> 完成时异步执行的延续任务。Creates a continuation that executes asynchronously when the target Task<TResult> completes.

ContinueWith(Action<Task>) ContinueWith(Action<Task>) ContinueWith(Action<Task>) ContinueWith(Action<Task>)

创建一个在目标 Task 完成时异步执行的延续任务。Creates a continuation that executes asynchronously when the target Task completes.

(Inherited from Task)
ContinueWith(Action<Task>, CancellationToken) ContinueWith(Action<Task>, CancellationToken) ContinueWith(Action<Task>, CancellationToken) ContinueWith(Action<Task>, CancellationToken)

创建一个在目标 Task 完成时可接收取消标记并以异步方式执行的延续任务。Creates a continuation that receives a cancellation token and executes asynchronously when the target Task completes.

(Inherited from Task)
ContinueWith(Action<Task>, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith(Action<Task>, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith(Action<Task>, CancellationToken, TaskContinuationOptions, TaskScheduler) 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.

(Inherited from Task)
ContinueWith(Action<Task>, TaskContinuationOptions) ContinueWith(Action<Task>, TaskContinuationOptions) ContinueWith(Action<Task>, TaskContinuationOptions) ContinueWith(Action<Task>, TaskContinuationOptions)

创建一个在目标任务完成时按照指定的 TaskContinuationOptions 执行的延续任务。Creates a continuation that executes when the target task completes according to the specified TaskContinuationOptions.

(Inherited from Task)
ContinueWith(Action<Task>, TaskScheduler) ContinueWith(Action<Task>, TaskScheduler) ContinueWith(Action<Task>, TaskScheduler) ContinueWith(Action<Task>, TaskScheduler)

创建一个在目标 Task 完成时异步执行的延续任务。Creates a continuation that executes asynchronously when the target Task completes. 延续任务使用指定计划程序。The continuation uses a specified scheduler.

(Inherited from Task)
ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object) ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object) ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object) 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) ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken) ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken) 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) ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) 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) ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskContinuationOptions) ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskContinuationOptions) 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) ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskScheduler) ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskScheduler) 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>) ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>) ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>) 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) ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken) ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken) 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) ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) 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) ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskContinuationOptions) ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskContinuationOptions) 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) ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskScheduler) ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskScheduler) 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) ContinueWith<TResult>(Func<Task,Object,TResult>, Object) ContinueWith<TResult>(Func<Task,Object,TResult>, Object) 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.

(Inherited from Task)
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken) ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken) ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken) 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.

(Inherited from Task)
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) 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.

(Inherited from Task)
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskContinuationOptions) ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskContinuationOptions) ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskContinuationOptions) 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.

(Inherited from Task)
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskScheduler) ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskScheduler) ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskScheduler) 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.

(Inherited from Task)
ContinueWith<TResult>(Func<Task,TResult>) ContinueWith<TResult>(Func<Task,TResult>) ContinueWith<TResult>(Func<Task,TResult>) ContinueWith<TResult>(Func<Task,TResult>)

创建一个在目标 Task<TResult> 完成时异步执行并返回一个值的延续任务。Creates a continuation that executes asynchronously when the target Task<TResult> completes and returns a value.

(Inherited from Task)
ContinueWith<TResult>(Func<Task,TResult>, CancellationToken) ContinueWith<TResult>(Func<Task,TResult>, CancellationToken) ContinueWith<TResult>(Func<Task,TResult>, CancellationToken) 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.

(Inherited from Task)
ContinueWith<TResult>(Func<Task,TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith<TResult>(Func<Task,TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) ContinueWith<TResult>(Func<Task,TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) 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.

(Inherited from Task)
ContinueWith<TResult>(Func<Task,TResult>, TaskContinuationOptions) ContinueWith<TResult>(Func<Task,TResult>, TaskContinuationOptions) ContinueWith<TResult>(Func<Task,TResult>, TaskContinuationOptions) ContinueWith<TResult>(Func<Task,TResult>, TaskContinuationOptions)

创建一个按照指定延续任务选项执行并返回一个值的延续任务。Creates a continuation that executes according to the specified continuation options and returns a value.

(Inherited from Task)
ContinueWith<TResult>(Func<Task,TResult>, TaskScheduler) ContinueWith<TResult>(Func<Task,TResult>, TaskScheduler) ContinueWith<TResult>(Func<Task,TResult>, TaskScheduler) 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.

(Inherited from Task)
Dispose() Dispose() Dispose() Dispose()

释放 Task 类的当前实例所使用的所有资源。Releases all resources used by the current instance of the Task class.

(Inherited from Task)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

释放 Task,同时释放其所有非托管资源。Disposes the Task, releasing all of its unmanaged resources.

(Inherited from Task)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetAwaiter() GetAwaiter() GetAwaiter() GetAwaiter()

获取用于等待此 Task<TResult> 的等待者。Gets an awaiter used to await this Task<TResult>.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
RunSynchronously() RunSynchronously() RunSynchronously() RunSynchronously()

对当前的 Task 同步运行 TaskSchedulerRuns the Task synchronously on the current TaskScheduler.

(Inherited from Task)
RunSynchronously(TaskScheduler) RunSynchronously(TaskScheduler) RunSynchronously(TaskScheduler) RunSynchronously(TaskScheduler)

对提供的 Task 同步运行 TaskSchedulerRuns the Task synchronously on the TaskScheduler provided.

(Inherited from Task)
Start() Start() Start() Start()

启动 Task,并将它安排到当前的 TaskScheduler 中执行。Starts the Task, scheduling it for execution to the current TaskScheduler.

(Inherited from Task)
Start(TaskScheduler) Start(TaskScheduler) Start(TaskScheduler) Start(TaskScheduler)

启动 Task,并将它安排到指定的 TaskScheduler 中执行。Starts the Task, scheduling it for execution to the specified TaskScheduler.

(Inherited from Task)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
Wait() Wait() Wait() Wait()

等待 Task 完成执行过程。Waits for the Task to complete execution.

(Inherited from Task)
Wait(CancellationToken) Wait(CancellationToken) Wait(CancellationToken) Wait(CancellationToken)

等待 Task 完成执行过程。Waits for the Task to complete execution. 如果在任务完成之前取消标记已取消,等待将终止。The wait terminates if a cancellation token is canceled before the task completes.

(Inherited from Task)
Wait(Int32) Wait(Int32) Wait(Int32) Wait(Int32)

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

(Inherited from Task)
Wait(Int32, CancellationToken) Wait(Int32, CancellationToken) Wait(Int32, CancellationToken) 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.

(Inherited from Task)
Wait(TimeSpan) Wait(TimeSpan) Wait(TimeSpan) Wait(TimeSpan)

等待 Task 在指定的时间间隔内完成执行。Waits for the Task to complete execution within a specified time interval.

(Inherited from Task)

Extension Methods

Unwrap(Task<Task>) Unwrap(Task<Task>) Unwrap(Task<Task>) 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>>) Unwrap<TResult>(Task<Task<TResult>>) Unwrap<TResult>(Task<Task<TResult>>) Unwrap<TResult>(Task<Task<TResult>>)

创建一个表示 Task 的异步操作的代理 Task<Task<T>> (C#) or Task (Of Task(Of T)) (Visual Basic)。 Creates a proxy Task that represents the asynchronous operation of a Task<Task<T>> (C#) or Task (Of Task(Of T)) (Visual Basic).

DispatcherOperationWait(Task) DispatcherOperationWait(Task) DispatcherOperationWait(Task) DispatcherOperationWait(Task)

无限期等待完成基础 DispatcherOperationWaits indefinitely for the underlying DispatcherOperation to complete.

DispatcherOperationWait(Task, TimeSpan) DispatcherOperationWait(Task, TimeSpan) DispatcherOperationWait(Task, TimeSpan) DispatcherOperationWait(Task, TimeSpan)

等待基础 DispatcherOperation 完成,直到达到指定的所需时间。Waits for the specified amount of time for the underlying DispatcherOperation to complete.

IsDispatcherOperationTask(Task) IsDispatcherOperationTask(Task) IsDispatcherOperationTask(Task) IsDispatcherOperationTask(Task)

返回一个值,该值指示此 Task 是否与 DispatcherOperation 相关。Returns a value that indicates whether this Task is associated with a DispatcherOperation.

AsAsyncAction(Task) AsAsyncAction(Task) AsAsyncAction(Task) AsAsyncAction(Task)

返回表示已开始任务的 Windows 运行时异步操作。Returns a Windows Runtime asynchronous action that represents a started task.

AsAsyncOperation<TResult>(Task<TResult>) AsAsyncOperation<TResult>(Task<TResult>) AsAsyncOperation<TResult>(Task<TResult>) 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.

另请参阅