## Wait(TimeSpan)

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

#### 例外狀況

`timeout` 為 -1 毫秒以外的負數，表示無限逾時。`timeout` is a negative number other than -1 milliseconds, which represents an infinite time-out. -或--or- `timeout` 大於 MaxValue`timeout` is greater than MaxValue.

-或--or- 在工作執行期間擲回例外狀況。An exception was thrown during the execution of the task. InnerExceptions 集合包含例外狀況的相關資訊。The InnerExceptions collection contains information about the exception or exceptions.

### 範例

``````using System;

public class Example
{
public static void Main()
{
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 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 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)

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

#### 參數

millisecondsTimeout
Int32

cancellationToken
CancellationToken

#### 例外狀況

`millisecondsTimeout` 為 -1 以外的負數，表示無限逾時。`millisecondsTimeout` is a negative number other than -1, which represents an infinite time-out.

-或--or- 在工作執行期間擲回例外狀況。An exception was thrown during the execution of the task. InnerExceptions 集合包含例外狀況的相關資訊。The InnerExceptions collection contains information about the exception or exceptions.

### 範例

``````using System;

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

});
try {
bool result = t.Wait(1510, ts.Token);
Console.WriteLine("Wait completed normally: {0}", result);
}
catch (OperationCanceledException e) {
Console.WriteLine("{0}: The wait has been canceled. Task status: {1:G}",
e.GetType().Name, t.Status);
Console.WriteLine("After sleeping, the task status:  {0:G}", t.Status);
ts.Dispose();
}
}

private static void CancelToken(Object obj)
{
Console.WriteLine("Canceling the cancellation token from thread {0}...",
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:
//    Canceling the cancellation token from thread 3...
//    OperationCanceledException: The wait has been canceled. Task status: Running
//    After sleeping, the task status:  RanToCompletion
// The example displays output like the following if the wait is canceled by
// the timeout interval expiring:
//    Wait completed normally: False
//    Canceling the cancellation token from thread 3...
``````
``````Imports System.Threading

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

End Sub)
Try
Dim result As Boolean = t.Wait(1510, ts.Token)
Console.WriteLine("Wait completed normally: {0}", result)
Catch e As OperationCanceledException
Console.WriteLine("{0}: The wait has been canceled. Task status: {1:G}",
e.GetType().Name, t.Status)
Console.WriteLine("After sleeping, the task status:  {0:G}", t.Status)
ts.Dispose()
End Try
End Sub

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

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:
'    Canceling the cancellation token from thread 3...
'    OperationCanceledException: The wait has been canceled. Task status: Running
'    After sleeping, the task status:  RanToCompletion
' The example displays output like the following if the wait is canceled by
' the timeout interval expiring:
'    Wait completed normally: False
'    Canceling the cancellation token from thread 3...
``````

### 備註

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:

• 工作本身已取消或擲回例外狀況。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.

• 已取消 `cancellationToken` 解除標記。The `cancellationToken` cancellation token is canceled. 在此情況下，呼叫 Wait(Int32, CancellationToken) 方法會擲回 OperationCanceledExceptionIn this case, the call to the Wait(Int32, CancellationToken) method throws an OperationCanceledException.

• `millisecondsTimeout` 所定義的間隔。The interval defined by `millisecondsTimeout` elapses. 在此情況下，目前的執行緒會繼續執行，且方法會傳回 `false`In this case, the current thread resumes execution and the method returns `false`.

## Wait(CancellationToken)

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

#### 參數

cancellationToken
CancellationToken

#### 例外狀況

-或--or- 在工作執行期間擲回例外狀況。An exception was thrown during the execution of the task. InnerExceptions 集合包含例外狀況的相關資訊。The InnerExceptions collection contains information about the exception or exceptions.

### 範例

``````using System;

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

ts.Cancel();
});
try {
t.Wait(ts.Token);
}
catch (OperationCanceledException e) {
Console.WriteLine("{0}: The wait has been canceled. Task status: {1:G}",
e.GetType().Name, t.Status);
Console.WriteLine("After sleeping, the task status:  {0:G}", t.Status);
}
ts.Dispose();
}
}
// The example displays output like the following:
//    Calling Cancel...
//    OperationCanceledException: The wait has been canceled. Task status: Running
//    After sleeping, the task status:  RanToCompletion
``````
``````Imports System.Threading

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

Console.WriteLine("Calling Cancel...")
ts.Cancel()
End Sub)
Try
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)
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:
'    Calling Cancel...
'    OperationCanceledException: The wait has been canceled. Task status: Running
'    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:

## Wait(Int32)

``````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

#### 例外狀況

`millisecondsTimeout` 為 -1 以外的負數，表示無限逾時。`millisecondsTimeout` is a negative number other than -1, which represents an infinite time-out.

-或--or- 在工作執行期間擲回例外狀況。An exception was thrown during the execution of the task. InnerExceptions 集合包含例外狀況的相關資訊。The InnerExceptions collection contains information about the exception or exceptions.

### 範例

``````using System;

public class Example
{
public static void Main()
{
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 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 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()

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

#### 例外狀況

-或--or- 在工作執行期間擲回例外狀況。An exception was thrown during the execution of the task. InnerExceptions 集合包含例外狀況的相關資訊。The InnerExceptions collection contains information about the exception or exceptions.

### 範例

``````using System;

public class Example
{
public static void Main()
{
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 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
``````