Task.Wait Task.Wait Task.Wait Task.Wait Method

定义

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

重载

Wait(TimeSpan) Wait(TimeSpan) Wait(TimeSpan) Wait(TimeSpan)

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

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.

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.

Wait(Int32) Wait(Int32) Wait(Int32) Wait(Int32)

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

Wait() Wait() Wait() Wait()

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

Wait(TimeSpan) Wait(TimeSpan) Wait(TimeSpan) Wait(TimeSpan)

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

public:
 bool Wait(TimeSpan timeout);
public bool Wait (TimeSpan timeout);
member this.Wait : TimeSpan -> bool
Public Function Wait (timeout As TimeSpan) As Boolean

参数

timeout
TimeSpan TimeSpan TimeSpan TimeSpan

表示等待毫秒数的 TimeSpan,或表示 -1 毫秒(无限期等待)的 TimeSpanA TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.

返回

如果在分配的时间内 true 完成执行,则为 Task;否则为 falsetrue if the Task completed execution within the allotted time; otherwise, false.

异常

timeout 为 -1 毫秒以外的负数,表示无限期超时。timeout is a negative number other than -1 milliseconds, which represents an infinite time-out. - 或 --or- timeout 大于 MaxValuetimeout is greater than MaxValue.

已取消任务。The task was canceled. InnerExceptions 集合包含 TaskCanceledException 对象。The InnerExceptions collection contains a TaskCanceledException object.

-or- 执行任务期间引发了一个异常。An exception was thrown during the execution of the task. InnerExceptions 集合包含一个或多个异常的相关信息。The InnerExceptions collection contains information about the exception or exceptions.

示例

下面的示例启动了一个任务,该任务生成0到100之间的5000000随机整数,并计算其平均值。The following example starts a task that generates five million random integers between 0 and 100 and computes their mean. 该示例使用Wait(TimeSpan)方法等待应用程序在150毫秒内完成。The example uses the Wait(TimeSpan) method to wait for the application to complete within 150 milliseconds. 如果应用程序正常完成,任务会显示其生成的随机数字的总和和平均值。If the application completes normally, the task displays the sum and mean of the random numbers that it has generated. 如果超时间隔已过,则该示例在消息终止之前显示一条消息。If the timeout interval has elapsed, the example displays a message before it terminates.

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Task t = Task.Run( () => {
                            Random rnd = new Random();
                            long sum = 0;
                            int n = 5000000;
                            for (int ctr = 1; ctr <= n; ctr++) {
                               int number = rnd.Next(0, 101);
                               sum += number;
                            }
                            Console.WriteLine("Total:   {0:N0}", sum);
                            Console.WriteLine("Mean:    {0:N2}", sum/n);
                            Console.WriteLine("N:       {0:N0}", n);   
                         } );
     TimeSpan ts = TimeSpan.FromMilliseconds(150);
     if (! t.Wait(ts))
        Console.WriteLine("The timeout interval elapsed.");
   }
}
// The example displays output similar to the following:
//       Total:   50,015,714
//       Mean:    50.02
//       N:       1,000,000
// Or it displays the following output:
//      The timeout interval elapsed.
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim t As Task = Task.Run( Sub()
                                   Dim rnd As New Random()
                                   Dim sum As Long
                                   Dim n As Integer = 5000000
                                   For ctr As Integer = 1 To n
                                      Dim number As Integer = rnd.Next(0, 101)
                                      sum += number
                                   Next
                                   Console.WriteLine("Total:   {0:N0}", sum)
                                   Console.WriteLine("Mean:    {0:N2}", sum/n)
                                   Console.WriteLine("N:       {0:N0}", n)   
                                End Sub)
     Dim ts As TimeSpan = TimeSpan.FromMilliseconds(150)
     If Not t.Wait(ts) Then
        Console.WriteLine("The timeout interval elapsed.")
     End If
   End Sub
End Module
' The example displays output similar to the following:
'       Total:   50,015,714
'       Mean:    50.02
'       N:       1,000,000
' Or it displays the following output:
'       The timeout interval elapsed.

注解

Wait(TimeSpan)是一种同步方法,该方法使调用线程等待当前任务实例完成,直到发生以下情况之一:Wait(TimeSpan) is a synchronization method that causes the calling thread to wait for the current task instance to complete until one of the following occurs:

  • 任务成功完成。The task completes successfully.

  • 任务本身被取消或引发异常。The task itself is canceled or throws an exception. 在这种情况下,您AggregateException将处理异常。In this case, you handle an AggregateException exception. AggregateException.InnerExceptions属性包含有关异常的详细信息。The AggregateException.InnerExceptions property contains details about the exception or exceptions.

  • 所定义timeout的间隔。The interval defined by timeout elapses. 在这种情况下,当前线程将继续执行,并且false该方法将返回。In this case, the current thread resumes execution and the method returns false.

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.

public:
 bool Wait(int millisecondsTimeout, System::Threading::CancellationToken cancellationToken);
public bool Wait (int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
member this.Wait : int * System.Threading.CancellationToken -> bool

参数

millisecondsTimeout
Int32 Int32 Int32 Int32

等待的毫秒数,或为 Infinite (-1),表示无限期等待。The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

cancellationToken
CancellationToken CancellationToken CancellationToken CancellationToken

等待任务完成期间要观察的取消标记。A cancellation token to observe while waiting for the task to complete.

返回

如果在分配的时间内 true 完成执行,则为 Task;否则为 falsetrue if the Task completed execution within the allotted time; otherwise, false.

异常

已取消 cancellationTokenThe cancellationToken was canceled.

millisecondsTimeout 是一个非 -1 的负数,而 -1 表示无限期超时。millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

已取消任务。The task was canceled. InnerExceptions 集合包含 TaskCanceledException 对象。The InnerExceptions collection contains a TaskCanceledException object.

-or- 执行任务期间引发了一个异常。An exception was thrown during the execution of the task. InnerExceptions 集合包含一个或多个异常的相关信息。The InnerExceptions collection contains information about the exception or exceptions.

示例

下面的示例调用Wait(Int32, CancellationToken)方法,以提供可结束任务完成等待的超时值和取消标记。The following example calls the Wait(Int32, CancellationToken) method to provide both a timeout value and a cancellation token that can end the wait for a task's completion. 此时将启动一个新线程并执行CancelToken方法,该方法将暂停,然后CancellationTokenSource.Cancel调用方法以取消取消标记。A new thread is started and executes the CancelToken method, which pauses and then calls the CancellationTokenSource.Cancel method to cancel the cancellation tokens. 然后,将启动任务并延迟5秒。A task is then launched and delays for 5 seconds. 然后Wait调用方法来等待任务完成,并同时提供一个简短的超时值和一个取消标记。The Wait method is then called to wait for the task's completion and is provided both a brief timeout value and a cancellation token.

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

public class Example
{
   public static void Main()
   {
      CancellationTokenSource ts = new CancellationTokenSource();
      Thread thread = new Thread(CancelToken);
      thread.Start(ts);

      Task t = Task.Run( () => { Task.Delay(5000).Wait();
                                 Console.WriteLine("Task ended delay...");
                               });
      try {
         Console.WriteLine("About to wait completion of task {0}", t.Id);
         bool result = t.Wait(1510, ts.Token);
         Console.WriteLine("Wait completed normally: {0}", result);
         Console.WriteLine("The task status:  {0:G}", t.Status);
      }
      catch (OperationCanceledException e) {
         Console.WriteLine("{0}: The wait has been canceled. Task status: {1:G}",
                           e.GetType().Name, t.Status);
         Thread.Sleep(4000);
         Console.WriteLine("After sleeping, the task status:  {0:G}", t.Status);
         ts.Dispose();
      }
   }

   private static void CancelToken(Object obj)
   {
      Thread.Sleep(1500);
      Console.WriteLine("Canceling the cancellation token from thread {0}...",
                        Thread.CurrentThread.ManagedThreadId);
      CancellationTokenSource source = obj as CancellationTokenSource;
      if (source != null) source.Cancel();
   }
}
// The example displays output like the following if the wait is canceled by
// the cancellation token:
//    About to wait completion of task 1
//    Canceling the cancellation token from thread 3...
//    OperationCanceledException: The wait has been canceled. Task status: Running
//    Task ended delay...
//    After sleeping, the task status:  RanToCompletion
// The example displays output like the following if the wait is canceled by
// the timeout interval expiring:
//    About to wait completion of task 1
//    Wait completed normally: False
//    The task status:  Running
//    Canceling the cancellation token from thread 3...
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim ts As New CancellationTokenSource()
      Dim thread As New Thread(AddressOf CancelToken)
      thread.Start(ts)

      Dim t As Task = Task.Run( Sub()
                                   Task.Delay(5000).Wait()
                                    Console.WriteLine("Task ended delay...")
                                End Sub)
      Try
         Console.WriteLine("About to wait completion of task {0}", t.Id)
         Dim result As Boolean = t.Wait(1510, ts.Token)
         Console.WriteLine("Wait completed normally: {0}", result)
         Console.WriteLine("The task status:  {0:G}", t.Status)
      Catch e As OperationCanceledException
         Console.WriteLine("{0}: The wait has been canceled. Task status: {1:G}",
                           e.GetType().Name, t.Status)
         Thread.Sleep(4000)
         Console.WriteLine("After sleeping, the task status:  {0:G}", t.Status)
         ts.Dispose()
      End Try
   End Sub

   Private Sub CancelToken(obj As Object)
      Thread.Sleep(1500)
      Console.WriteLine("Canceling the cancellation token from thread {0}...",
                        Thread.CurrentThread.ManagedThreadId)

      If TypeOf obj Is CancellationTokenSource Then
         Dim source As CancellationTokenSource = CType(obj, CancellationTokenSource)
         source.Cancel()
      End If
   End Sub
End Module
' The example displays output like the following if the wait is canceled by
' the cancellation token:
'    About to wait completion of task 1
'    Canceling the cancellation token from thread 3...
'    OperationCanceledException: The wait has been canceled. Task status: Running
'    Task ended delay...
'    After sleeping, the task status:  RanToCompletion
' The example displays output like the following if the wait is canceled by
' the timeout interval expiring:
'    About to wait completion of task 1
'    Wait completed normally: False
'    The task status:  Running
'    Canceling the cancellation token from thread 3...

请注意,该示例的精确输出取决于是否由于取消标记而取消等待,或由于超时间隔已用。Note that the precise output from the example depends on whether the wait was canceled because of the cancellation token or because the timeout interval elapsed.

注解

Wait(Int32, CancellationToken)是一种同步方法,该方法使调用线程等待当前任务实例完成,直到发生以下情况之一:Wait(Int32, CancellationToken) is a synchronization method that causes the calling thread to wait for the current task instance to complete until one of the following occurs:

备注

cancellationToken取消取消标记不会对正在运行的任务产生任何影响,除非它还传递了取消标记并准备处理取消。Canceling the cancellationToken cancellation token has no effect on the running task unless it has also been passed the cancellation token and is prepared to handle cancellation. cancellationToken将对象传递给此方法只允许根据某些条件取消等待。Passing the cancellationToken object to this method simply allows the wait to be canceled based on some condition.

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.

public:
 void Wait(System::Threading::CancellationToken cancellationToken);
public void Wait (System.Threading.CancellationToken cancellationToken);
member this.Wait : System.Threading.CancellationToken -> unit

参数

cancellationToken
CancellationToken CancellationToken CancellationToken CancellationToken

等待任务完成期间要观察的取消标记。A cancellation token to observe while waiting for the task to complete.

异常

已取消 cancellationTokenThe cancellationToken was canceled.

已取消任务。The task was canceled. InnerExceptions 集合包含 TaskCanceledException 对象。The InnerExceptions collection contains a TaskCanceledException object.

-or- 执行任务期间引发了一个异常。An exception was thrown during the execution of the task. InnerExceptions 集合包含一个或多个异常的相关信息。The InnerExceptions collection contains information about the exception or exceptions.

示例

下面的示例演示如何使用取消标记来取消等待任务完成。The following example illustrates the simple use of a cancellation token to cancel waiting for a task's completion. 启动任务后,调用CancellationTokenSource.Cancel方法以取消标记源的任何取消标记,然后延迟五秒。A task is launched, calls the CancellationTokenSource.Cancel method to cancel any of the token source's cancellation tokens, and then delays for five seconds. 请注意,尚未向任务本身传递取消标记,因而不可取消。Note that the task itself has not been passed the cancellation token and is not cancelable. 应用程序线程调用任务的Task.Wait方法来等待任务完成,但一旦取消标记OperationCanceledException并引发,就会取消等待。The application thread calls the task's Task.Wait method to wait for the task to complete, but the wait is canceled once the cancellation token is cancelled and an OperationCanceledException is thrown. 异常处理程序将报告异常,并休眠六秒钟。The exception handler reports the exception and then sleeps for six seconds. 如示例的输出所示,该延迟使任务在RanToCompletion状态中完成。As the output from the example shows, that delay allows the task to complete in the RanToCompletion state.

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

public class Example
{
   public static void Main()
   {
      CancellationTokenSource ts = new CancellationTokenSource();

      Task t = Task.Run( () => { Console.WriteLine("Calling Cancel...");
                                 ts.Cancel();
                                 Task.Delay(5000).Wait();
                                 Console.WriteLine("Task ended delay...");
                               });
      try {
         Console.WriteLine("About to wait for the task to complete...");
         t.Wait(ts.Token);
      }
      catch (OperationCanceledException e) {
         Console.WriteLine("{0}: The wait has been canceled. Task status: {1:G}",
                           e.GetType().Name, t.Status);
         Thread.Sleep(6000);
         Console.WriteLine("After sleeping, the task status:  {0:G}", t.Status);
      }
      ts.Dispose();
   }
}
// The example displays output like the following:
//    About to wait for the task to complete...
//    Calling Cancel...
//    OperationCanceledException: The wait has been canceled. Task status: Running
//    Task ended delay...
//    After sleeping, the task status:  RanToCompletion
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim ts As New CancellationTokenSource()

      Dim t = Task.Run( Sub()
                           Console.WriteLine("Calling Cancel...")
                           ts.Cancel()
                           Task.Delay(5000).Wait()
                           Console.WriteLine("Task ended delay...")
                        End Sub)
      Try
         Console.WriteLine("About to wait for the task to complete...")
         t.Wait(ts.Token)
      Catch e As OperationCanceledException
         Console.WriteLine("{0}: The wait has been canceled. Task status: {1:G}",
                           e.GetType().Name, t.Status)
         Thread.Sleep(6000)
         Console.WriteLine("After sleeping, the task status:  {0:G}", t.Status)
      End Try
      ts.Dispose()
   End Sub
End Module
' The example displays output like the following:
'    About to wait for the task to complete...
'    Calling Cancel...
'    OperationCanceledException: The wait has been canceled. Task status: Running
'    Task ended delay...
'    After sleeping, the task status:  RanToCompletion

注解

Wait(CancellationToken)方法创建可取消的等待; 也就是说,它会导致当前线程等待,直到发生以下情况之一:The Wait(CancellationToken) method creates a cancelable wait; that is, it causes the current thread to wait until one of the following occurs:

备注

cancellationToken取消取消标记不会对正在运行的任务产生任何影响,除非它还传递了取消标记并准备处理取消。Canceling the cancellationToken cancellation token has no effect on the running task unless it has also been passed the cancellation token and is prepared to handle cancellation. cancellationToken将对象传递到此方法只是允许取消等待。Passing the cancellationToken object to this method simply allows the wait to be canceled.

Wait(Int32) Wait(Int32) Wait(Int32) Wait(Int32)

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

public:
 bool Wait(int millisecondsTimeout);
public bool Wait (int millisecondsTimeout);
member this.Wait : int -> bool
Public Function Wait (millisecondsTimeout As Integer) As Boolean

参数

millisecondsTimeout
Int32 Int32 Int32 Int32

等待的毫秒数,或为 Infinite (-1),表示无限期等待。The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

返回

如果在分配的时间内 true 完成执行,则为 Task;否则为 falsetrue if the Task completed execution within the allotted time; otherwise, false.

异常

millisecondsTimeout 是一个非 -1 的负数,而 -1 表示无限期超时。millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

已取消任务。The task was canceled. InnerExceptions 集合包含 TaskCanceledException 对象。The InnerExceptions collection contains a TaskCanceledException object.

-or- 执行任务期间引发了一个异常。An exception was thrown during the execution of the task. InnerExceptions 集合包含一个或多个异常的相关信息。The InnerExceptions collection contains information about the exception or exceptions.

示例

下面的示例启动了一个任务,该任务生成0到100之间的5000000随机整数,并计算其平均值。The following example starts a task that generates five million random integers between 0 and 100 and computes their mean. 该示例使用Wait(Int32)方法等待应用程序在150毫秒内完成。The example uses the Wait(Int32) method to wait for the application to complete within 150 milliseconds. 如果应用程序正常完成,任务会显示其生成的随机数字的总和和平均值。If the application completes normally, the task displays the sum and mean of the random numbers that it has generated. 如果超时间隔已过,则该示例在消息终止之前显示一条消息。If the timeout interval has elapsed, the example displays a message before it terminates.

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Task t = Task.Run( () => {
                            Random rnd = new Random();
                            long sum = 0;
                            int n = 5000000;
                            for (int ctr = 1; ctr <= n; ctr++) {
                               int number = rnd.Next(0, 101);
                               sum += number;
                            }
                            Console.WriteLine("Total:   {0:N0}", sum);
                            Console.WriteLine("Mean:    {0:N2}", sum/n);
                            Console.WriteLine("N:       {0:N0}", n);   
                         } );
     if (! t.Wait(150))
        Console.WriteLine("The timeout interval elapsed.");
   }
}
// The example displays output similar to the following:
//       Total:   50,015,714
//       Mean:    50.02
//       N:       1,000,000
// Or it displays the following output:
//      The timeout interval elapsed.
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim t As Task = Task.Run( Sub()
                                   Dim rnd As New Random()
                                   Dim sum As Long
                                   Dim n As Integer = 5000000
                                   For ctr As Integer = 1 To n
                                      Dim number As Integer = rnd.Next(0, 101)
                                      sum += number
                                   Next
                                   Console.WriteLine("Total:   {0:N0}", sum)
                                   Console.WriteLine("Mean:    {0:N2}", sum/n)
                                   Console.WriteLine("N:       {0:N0}", n)   
                                End Sub)
     If Not t.Wait(150) Then
        Console.WriteLine("The timeout interval elapsed.")
     End If
   End Sub
End Module
' The example displays output similar to the following:
'       Total:   50,015,714
'       Mean:    50.02
'       N:       1,000,000
' Or it displays the following output:
'       The timeout interval elapsed.

注解

Wait(Int32)是一种同步方法,该方法使调用线程等待当前任务实例完成,直到发生以下情况之一:Wait(Int32) is a synchronization method that causes the calling thread to wait for the current task instance to complete until one of the following occurs:

  • 任务成功完成。The task completes successfully.

  • 任务本身被取消或引发异常。The task itself is canceled or throws an exception. 在这种情况下,您AggregateException将处理异常。In this case, you handle an AggregateException exception. AggregateException.InnerExceptions属性包含有关异常的详细信息。The AggregateException.InnerExceptions property contains details about the exception or exceptions.

  • 所定义millisecondsTimeout的间隔。The interval defined by millisecondsTimeout elapses. 在这种情况下,当前线程将继续执行,并且false该方法将返回。In this case, the current thread resumes execution and the method returns false.

Wait() Wait() Wait() Wait()

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

public:
 void Wait();
public void Wait ();
member this.Wait : unit -> unit
Public Sub Wait ()

异常

已取消任务。The task was canceled. InnerExceptions 集合包含 TaskCanceledException 对象。The InnerExceptions collection contains a TaskCanceledException object.

-or- 执行任务期间引发了一个异常。An exception was thrown during the execution of the task. InnerExceptions 集合包含一个或多个异常的相关信息。The InnerExceptions collection contains information about the exception or exceptions.

示例

下面的示例启动了一个任务,该任务生成0到100之间的1000000随机整数,并计算其平均值。The following example starts a task that generates one million random integers between 0 and 100 and computes their mean. 该示例使用Wait方法来确保该任务在应用程序终止前完成。The example uses the Wait method to ensure that the task completes before the application terminates. 否则,因为这是一个控制台应用程序,则在任务可以计算并显示平均值之前,此示例将终止。Otherwise, because this is a console application, the example would terminate before the task can compute and display the mean.

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Task t = Task.Run( () => {
                            Random rnd = new Random();
                            long sum = 0;
                            int n = 1000000;
                            for (int ctr = 1; ctr <= n; ctr++) {
                               int number = rnd.Next(0, 101);
                               sum += number;
                            }
                            Console.WriteLine("Total:   {0:N0}", sum);
                            Console.WriteLine("Mean:    {0:N2}", sum/n);
                            Console.WriteLine("N:       {0:N0}", n);   
                         } );
     t.Wait();
   }
}
// The example displays output similar to the following:
//       Total:   50,015,714
//       Mean:    50.02
//       N:       1,000,000
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim t As Task = Task.Run( Sub()
                                   Dim rnd As New Random()
                                   Dim sum As Long
                                   Dim n As Integer = 1000000
                                   For ctr As Integer = 1 To n
                                      Dim number As Integer = rnd.Next(0, 101)
                                      sum += number
                                   Next
                                   Console.WriteLine("Total:   {0:N0}", sum)
                                   Console.WriteLine("Mean:    {0:N2}", sum/n)
                                   Console.WriteLine("N:       {0:N0}", n)   
                                End Sub)
     t.Wait()
   End Sub
End Module
' The example displays output similar to the following:
'       Total:   50,015,714
'       Mean:    50.02
'       N:       1,000,000

注解

Wait同步方法,这会导致调用线程等待,直到当前任务完成。Wait is a synchronization method that causes the calling thread to wait until the current task has completed. 如果当前任务尚未开始执行,则 Wait 方法尝试从计划程序中删除任务并在当前线程上以内联方式执行该任务。If the current task has not started execution, the Wait method attempts to remove the task from the scheduler and execute it inline on the current thread. 如果无法执行此操作,或者当前任务已开始执行,则它会阻止调用线程,直到任务完成。If it is unable to do that, or if the current task has already started execution, it blocks the calling thread until the task completes. 有关详细信息,请参阅 "通过 .NET 并行编程" 博客中的"任务" 和 "内联"For more information, see Task.Wait and "Inlining" in the Parallel Programming with .NET blog.

另请参阅

适用于