Task.Wait 方法

定義

等候 Task 完成執行。

多載

Wait()

等候 Task 完成執行。

Wait(Int32)

等待 Task 在指定的毫秒數內完成執行。

Wait(CancellationToken)

等候 Task 完成執行。 如果在工作完成之前取消語彙基元已取消,則等候會終止。

Wait(TimeSpan)

等待 Task 在指定的時間間隔內完成執行。

Wait(Int32, CancellationToken)

等候 Task 完成執行。 如果在工作完成之前經過逾時間隔或取消語彙基元已取消,則等候會終止。

Wait(TimeSpan, CancellationToken)

等候 Task 完成執行。

Wait()

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

等候 Task 完成執行。

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

例外狀況

Task 已經處置。

工作已取消。 InnerExceptions 集合包含 TaskCanceledException 物件。

-或-

在工作執行期間擲回例外狀況。 InnerExceptions 集合包含例外狀況的相關資訊。

範例

下列範例會啟動一項工作,其會產生介於 0 到 100 之間的一百萬個隨機整數,並計算其平均數。 此範例會 Wait 使用 方法來確保工作在應用程式終止之前完成。 否則,因為這是主控台應用程式,所以範例會在工作可以計算並顯示平均值之前終止。

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
open System
open System.Threading.Tasks

let t =
    Task.Run(fun () ->
        let rnd = Random()
        let mutable sum = 0L
        let n = 1000000

        for _ = 1 to n do
            let number = rnd.Next(0, 101)
            sum <- sum + int64 number

        printfn $"Total:   {sum:N0}"
        printfn $"Mean:    {float sum / float n:N2}"
        printfn $"N:       {n:N0}")

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 方法會嘗試從排程器中移除工作,並在目前的執行緒上內嵌執行。 如果無法這麼做,或目前的工作已經開始執行,它會封鎖呼叫執行緒,直到工作完成為止。 如需詳細資訊,請參閱使用 .NET 平行程式設計部落格中的 Task.Wait 和「Inlining 」。

另請參閱

適用於

Wait(Int32)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

等待 Task 在指定的毫秒數內完成執行。

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)。

傳回

如果 true 在指定的時間內執行完成,則為 Task,否則為 false

例外狀況

Task 已經處置。

millisecondsTimeout 為 -1 以外的負數,表示無限逾時。

工作已取消。 InnerExceptions 集合包含 TaskCanceledException 物件。

-或-

在工作執行期間擲回例外狀況。 InnerExceptions 集合包含例外狀況的相關資訊。

範例

下列範例會啟動一項工作,其會產生介於 0 到 100 之間的五百萬個隨機整數,並計算其平均數。 此範例會 Wait(Int32) 使用 方法來等候應用程式在 150 毫秒內完成。 如果應用程式正常完成,工作會顯示其產生的亂數總和和和平均值。 如果逾時間隔已過,此範例會在終止之前顯示訊息。

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.
open System
open System.Threading.Tasks

let t =
    Task.Run(fun () ->
        let rnd = Random()
        let mutable sum = 0L
        let n = 5000000

        for _ = 1 to n do
            let number = rnd.Next(0, 101)
            sum <- sum + int64 number

        printfn $"Total:   {sum:N0}"
        printfn $"Mean:    {float sum / float n:N2}"
        printfn $"N:       {n:N0}")

if t.Wait 150 |> not then
    printfn "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) 是一種同步處理方法,可讓呼叫執行緒等候目前的工作實例完成,直到發生下列其中一項為止:

  • 工作成功完成。

  • 工作本身已取消或擲回例外狀況。 在此情況下,您會處理例外狀況 AggregateException 。 屬性 AggregateException.InnerExceptions 包含例外狀況或例外狀況的詳細資料。

  • 由經過定義的 millisecondsTimeout 間隔。 在此情況下,目前的執行緒會繼續執行,而 方法會傳 false 回 。

適用於

Wait(CancellationToken)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

等候 Task 完成執行。 如果在工作完成之前取消語彙基元已取消,則等候會終止。

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

參數

cancellationToken
CancellationToken

等候工作完成時要觀察的取消語彙基元。

例外狀況

已取消 cancellationToken

此工作已經處置。

工作已取消。 InnerExceptions 集合包含 TaskCanceledException 物件。

-或-

在工作執行期間擲回例外狀況。 InnerExceptions 集合包含例外狀況的相關資訊。

範例

下列範例說明解除標記的簡單用法,以取消等候工作的完成。 啟動工作、呼叫 CancellationTokenSource.Cancel 方法來取消任何權杖來源的取消權杖,然後延遲五秒。 請注意,工作本身尚未傳遞取消權杖,而且無法取消。 應用程式執行緒會呼叫工作的 Task.Wait 方法來等候工作完成,但在取消權杖取消並 OperationCanceledException 擲回 之後,就會取消等候。 例外狀況處理常式會報告例外狀況,然後睡眠六秒。 如範例的輸出所示,延遲可讓工作在 狀態中 RanToCompletion 完成。

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
open System
open System.Threading
open System.Threading.Tasks

let ts = new CancellationTokenSource()

let t =
    Task.Run(fun () ->
        printfn "Calling Cancel..."
        ts.Cancel()
        Task.Delay(5000).Wait()
        printfn $"Task ended delay...")

try
    printfn "About to wait for the task to complete..."
    t.Wait ts.Token

with :? OperationCanceledException as e ->
    printfn $"{e.GetType().Name}: The wait has been canceled. Task status: {t.Status:G}"
    Thread.Sleep 6000
    printfn $"After sleeping, the task status:  {t.Status:G}"

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) 會建立可取消的等候;也就是說,它會導致目前的執行緒等待下列其中一項發生:

注意

cancellationToken取消取消權杖不會影響執行中的工作,除非它也已通過取消權杖,並準備好處理取消。 將 cancellationToken 物件傳遞至這個方法,只是允許取消等候。

適用於

Wait(TimeSpan)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

等待 Task 在指定的時間間隔內完成執行。

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 毫秒。

傳回

如果 true 在指定的時間內執行完成,則為 Task,否則為 false

例外狀況

Task 已經處置。

timeout 為 -1 毫秒以外的負數,表示無限逾時。

-或-

timeout 大於 Int32.MaxValue

工作已取消。 InnerExceptions 集合包含 TaskCanceledException 物件。

-或-

在工作執行期間擲回例外狀況。 InnerExceptions 集合包含例外狀況的相關資訊。

範例

下列範例會啟動一項工作,其會產生介於 0 到 100 之間的五百萬個隨機整數,並計算其平均數。 此範例會 Wait(TimeSpan) 使用 方法來等候應用程式在 150 毫秒內完成。 如果應用程式正常完成,工作會顯示其產生的亂數總和和和平均值。 如果逾時間隔已過,此範例會在終止之前顯示訊息。

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.
open System
open System.Threading.Tasks

let t =
    Task.Run(fun () ->
        let rnd = Random()
        let mutable sum = 0L
        let n = 5000000

        for _ = 1 to n do
            let number = rnd.Next(0, 101)
            sum <- sum + int64 number

        printfn $"Total:   {sum:N0}"
        printfn $"Mean:    {float sum / float n:N2}"
        printfn $"N:       {n:N0}")

let ts = TimeSpan.FromMilliseconds 150

if t.Wait ts |> not then
    printfn "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) 是一種同步處理方法,可讓呼叫執行緒等候目前的工作實例完成,直到發生下列其中一項為止:

  • 工作成功完成。

  • 工作本身已取消或擲回例外狀況。 在此情況下,您會處理例外狀況 AggregateException 。 屬性 AggregateException.InnerExceptions 包含例外狀況或例外狀況的詳細資料。

  • 由經過定義的 timeout 間隔。 在此情況下,目前的執行緒會繼續執行,而 方法會傳 false 回 。

適用於

Wait(Int32, CancellationToken)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

等候 Task 完成執行。 如果在工作完成之前經過逾時間隔或取消語彙基元已取消,則等候會終止。

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
Public Function Wait (millisecondsTimeout As Integer, cancellationToken As CancellationToken) As Boolean

參數

millisecondsTimeout
Int32

要等候的毫秒數,如果要無限期等候,則為 Infinite (-1)。

cancellationToken
CancellationToken

等候工作完成時要觀察的取消語彙基元。

傳回

如果 true 在指定的時間內執行完成,則為 Task,否則為 false

例外狀況

已取消 cancellationToken

Task 已經處置。

millisecondsTimeout 為 -1 以外的負數,表示無限逾時。

工作已取消。 InnerExceptions 集合包含 TaskCanceledException 物件。

-或-

在工作執行期間擲回例外狀況。 InnerExceptions 集合包含例外狀況的相關資訊。

範例

下列範例會呼叫 Wait(Int32, CancellationToken) 方法,以提供逾時值和解除標記,以結束工作完成的等候。 會啟動新的執行緒並執行 CancelToken 方法,該方法會暫停,然後呼叫 CancellationTokenSource.Cancel 方法來取消取消取消權杖。 接著會啟動工作,並延遲 5 秒。 Wait接著會呼叫 方法來等候工作的完成,並提供簡短的逾時值和解除標記。

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...
open System
open System.Threading
open System.Threading.Tasks

let cancelToken (obj: obj) =
    Thread.Sleep 1500
    printfn $"Canceling the cancellation token from thread {Thread.CurrentThread.ManagedThreadId}..."

    match obj with
    | :? CancellationTokenSource as source -> source.Cancel()
    | _ -> ()

let ts = new CancellationTokenSource()
let thread = Thread(ParameterizedThreadStart cancelToken)
thread.Start ts

let t =
    Task.Run(fun () ->
        Task.Delay(5000).Wait()
        printfn "Task ended delay...")

try
    printfn $"About to wait completion of task {t.Id}"
    let result = t.Wait(1510, ts.Token)
    printfn $"Wait completed normally: {result}"
    printfn $"The task status:  {t.Status:G}"

with :? OperationCanceledException as e ->
    printfn $"{e.GetType().Name}: The wait has been canceled. Task status: {t.Status:G}"
    Thread.Sleep 4000
    printfn $"After sleeping, the task status:  {t.Status:G}"
    ts.Dispose()

// 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...

請注意,範例的精確輸出取決於因為取消權杖而取消等候,還是因為逾時間隔已耗用。

備註

Wait(Int32, CancellationToken) 是一種同步處理方法,可讓呼叫執行緒等候目前的工作實例完成,直到發生下列其中一項為止:

注意

cancellationToken取消取消權杖不會影響執行中工作,除非它也已通過取消權杖,而且已準備好處理取消。 將 cancellationToken 物件傳遞至這個方法,只允許根據某些條件取消等候。

適用於

Wait(TimeSpan, CancellationToken)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

等候 Task 完成執行。

public:
 bool Wait(TimeSpan timeout, System::Threading::CancellationToken cancellationToken);
public bool Wait (TimeSpan timeout, System.Threading.CancellationToken cancellationToken);
member this.Wait : TimeSpan * System.Threading.CancellationToken -> bool
Public Function Wait (timeout As TimeSpan, cancellationToken As CancellationToken) As Boolean

參數

timeout
TimeSpan

等候的時間,或 InfiniteTimeSpan 無限期等候

cancellationToken
CancellationToken

等候 CancellationToken 工作完成時要觀察的 。

傳回

如果 true 在指定的時間內執行完成,則為 Task,否則為 false

例外狀況

Task已取消

-或-

在執行 期間擲回例外狀況 Task

timeout 是 -1 毫秒以外的負數,表示無限逾時

-或-

逾時大於 MaxValue

已取消 cancellationToken

適用於