Task.Wait メソッド
定義
オーバーロード
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(TimeSpan) |
提供された Task の実行が完了するまで、指定した時間間隔内の間、待機します。Waits for the Task to complete execution within a specified time interval. |
Wait(Int32) |
提供された Task の実行が完了するまで、指定したミリ秒数以内の間、待機します。Waits for the Task to complete execution within a specified number of milliseconds. |
Wait(CancellationToken) |
Task の実行が完了するまで待機します。Waits for the Task to complete execution. タスクの完了前にキャンセル トークンが取り消される場合は、待機が終了します。The wait terminates if a cancellation token is canceled before the task completes. |
Wait() |
Task の実行が完了するまで待機します。Waits for the Task to complete execution. |
Wait(Int32, CancellationToken)
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)。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。それ以外の場合は false
。true
if the Task completed execution within the allotted time; otherwise, false
.
例外
cancellationToken
は取り消されました。The cancellationToken
was canceled.
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 メソッドを呼び出してタスクの完了を待機し、短いタイムアウト値とキャンセルトークンの両方を提供します。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:
タスクが正常に完了しました。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.
cancellationToken
キャンセルトークンが取り消されました。ThecancellationToken
cancellation token is canceled. この場合、メソッドを呼び出すと、 Wait(Int32, CancellationToken) がスローさ OperationCanceledException れます。In this case, the call to the Wait(Int32, CancellationToken) method throws an OperationCanceledException.によって定義された間隔が
millisecondsTimeout
経過します。The interval defined bymillisecondsTimeout
elapses. この場合、現在のスレッドは実行を再開し、メソッドはを返しfalse
ます。In this case, the current thread resumes execution and the method returnsfalse
.
注意
キャンセルトークンをキャンセルしても、キャンセルトークンが 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(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。無制限に待機する場合は、-1 ミリ秒を表す TimeSpan。A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.
戻り値
割り当てられた時間内に true
の実行が完了した場合は Task。それ以外の場合は false
。true
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
が MaxValue より大きくなっています。timeout
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の間に500万のランダムな整数を生成し、その平均値を計算するタスクを開始します。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 bytimeout
elapses. この場合、現在のスレッドは実行を再開し、メソッドはを返しfalse
ます。In this case, the current thread resumes execution and the method returnsfalse
.
適用対象
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
待機するミリ秒数。無制限に待機する場合は Infinite (-1)。The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.
戻り値
割り当てられた時間内に true
の実行が完了した場合は Task。それ以外の場合は false
。true
if the Task completed execution within the allotted time; otherwise, false
.
例外
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の間に500万のランダムな整数を生成し、その平均値を計算するタスクを開始します。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 bymillisecondsTimeout
elapses. この場合、現在のスレッドは実行を再開し、メソッドはを返しfalse
ます。In this case, the current thread resumes execution and the method returnsfalse
.
適用対象
Wait(CancellationToken)
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
タスクの完了の待機中に観察するキャンセル トークン。A cancellation token to observe while waiting for the task to complete.
例外
cancellationToken
は取り消されました。The 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 てトークンソースのキャンセルトークンをキャンセルし、5秒間遅延します。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. 例外ハンドラーは例外を報告し、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:
タスクが完了します。The task completes.
キャンセルトークンが取り消されました。The cancellation token is canceled. この場合、メソッドを呼び出すと、 Wait(CancellationToken) がスローさ OperationCanceledException れます。In this case, the call to the Wait(CancellationToken) method throws an OperationCanceledException.
注意
キャンセルトークンをキャンセルしても、キャンセルトークンが 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()
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の間に100万のランダムな整数を生成し、その平均値を計算するタスクを開始します。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 を使用した並列プログラミング」ブログの「 Task. Wait」と「インライン展開」 を参照してください。For more information, see Task.Wait and "Inlining" in the Parallel Programming with .NET blog.