Task.Wait 方法

定義

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

多載

Wait(TimeSpan)

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

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)

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

Wait(Int32)

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

Wait()

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

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,代表無限期等候的 -1 毫秒。A 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.

例外狀況

Task 已經處置。The Task has been disposed.

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. 在此情況下,目前的執行緒會繼續執行,且方法會傳回 falseIn this case, the current thread resumes execution and the method returns false.

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

要等候的毫秒數,如果要無限期等候,則為 Infinite (-1)。The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

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.

Task 已經處置。The Task has been disposed.

millisecondsTimeout 為 -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 方法以等候工作完成,並提供簡短的 timeout 值和解除標記。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)

等候 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

等候工作完成時要觀察的取消語彙基元。A cancellation token to observe while waiting for the task to complete.

例外狀況

已取消 cancellationTokenThe cancellationToken was canceled.

此工作已經處置。The task has been disposed.

工作已取消。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 方法來等候工作完成,但解除標記之後就會取消等候,並擲回 OperationCanceledExceptionThe 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. 例外狀況處理常式會報告例外狀況,然後進入睡眠狀態6秒。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)

等待 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

要等候的毫秒數,如果要無限期等候,則為 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.

例外狀況

Task 已經處置。The Task has been disposed.

millisecondsTimeout 為 -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. 在此情況下,目前的執行緒會繼續執行,且方法會傳回 falseIn this case, the current thread resumes execution and the method returns false.

Wait()

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

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

例外狀況

Task 已經處置。The Task has been disposed.

工作已取消。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.

另請參閱

適用於