派生クラスでオーバーライドされると、現在のインスタンスがシグナルを受信するまで現在のスレッドをブロックします。このとき TimeSpan を使用して時間間隔を計測し、待機の前に同期ドメインを終了するかどうかを指定します。
Overloads Public Overridable Function WaitOne( _
ByVal timeout As TimeSpan, _ ByVal exitContext As Boolean _) As Boolean
[C#]
public virtual bool WaitOne(TimeSpantimeout,boolexitContext);
[C++]
public: virtual bool WaitOne(TimeSpantimeout,boolexitContext);
[JScript]
public function WaitOne(
timeout : TimeSpan,exitContext : Boolean) : Boolean;
パラメータ
- timeout
待機するミリ秒数。または無制限に待機するミリ秒数 -1 を表す TimeSpan 。 - exitContext
待機する前にコンテキストの同期ドメインを終了し (同期されたコンテキストの場合)、再び取得する場合は true 。それ以外の場合は false 。
戻り値
現在のインスタンスがシグナルを受け取る場合は true 。それ以外の場合は false 。
例外
例外の種類 | 条件 |
---|---|
ObjectDisposedException | 現在のインスタンスは既に破棄されています。 |
ArgumentOutOfRangeException | timeout が -1 ミリ秒以外の負数です。-1 は無制限のタイムアウトを表します。 |
解説
このメソッドの呼び出し元は、現在のインスタンスがシグナルを受け取るか、タイムアウトになるまで、無期限にブロックします。 WaitHandle が、非同期操作が完了したとき生成されるシグナルなどの、別のスレッドからのシグナルを受信するまで、このメソッドを使用してブロックします。詳細については、 System.IAsyncResult インターフェイスのトピックを参照してください。
このメソッドをオーバーライドして、派生クラスの動作をカスタマイズします。
使用例
[Visual Basic, C#, C++] 待機ハンドルを使用して、バックグラウンド スレッドの実行が終了するまでプロセスの終了を保留する方法の例を次に示します。
Imports System
Imports System.Threading
Public Class WaitOne
Shared autoEvent As New AutoResetEvent(False)
Shared Sub Main()
Console.WriteLine("Main starting.")
ThreadPool.QueueUserWorkItem(AddressOf WorkMethod, autoEvent)
' Wait for work method to signal.
If autoEvent.WaitOne(New TimeSpan(0, 0, 1), False) Then
Console.WriteLine("Work method signaled.")
Else
Console.WriteLine("Timed out waiting for work " & _
"method to signal.")
End If
Console.WriteLine("Main ending.")
End Sub
Shared Sub WorkMethod(stateInfo As Object)
Console.WriteLine("Work starting.")
' Simulate time spent working.
Thread.Sleep(New Random().Next(100, 2000))
' Signal that work is finished.
Console.WriteLine("Work ending.")
CType(stateInfo, AutoResetEvent).Set()
End Sub
End Class
[C#]
using System;
using System.Threading;
class WaitOne
{
static AutoResetEvent autoEvent = new AutoResetEvent(false);
static void Main()
{
Console.WriteLine("Main starting.");
ThreadPool.QueueUserWorkItem(
new WaitCallback(WorkMethod), autoEvent);
// Wait for work method to signal.
if(autoEvent.WaitOne(new TimeSpan(0, 0, 1), false))
{
Console.WriteLine("Work method signaled.");
}
else
{
Console.WriteLine("Timed out waiting for work " +
"method to signal.");
}
Console.WriteLine("Main ending.");
}
static void WorkMethod(object stateInfo)
{
Console.WriteLine("Work starting.");
// Simulate time spent working.
Thread.Sleep(new Random().Next(100, 2000));
// Signal that work is finished.
Console.WriteLine("Work ending.");
((AutoResetEvent)stateInfo).Set();
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
__gc class WaitOne
{
WaitOne() {}
public:
static void WorkMethod(Object* stateInfo)
{
Console::WriteLine(S"Work starting.");
// Simulate time spent working.
Thread::Sleep((new Random())->Next(100, 2000));
// Signal that work is finished.
Console::WriteLine(S"Work ending.");
dynamic_cast<AutoResetEvent*>(stateInfo)->Set();
}
};
void main()
{
Console::WriteLine(S"Main starting.");
AutoResetEvent* autoEvent = new AutoResetEvent(false);
ThreadPool::QueueUserWorkItem(
new WaitCallback(0, &WaitOne::WorkMethod), autoEvent);
// Wait for work method to signal.
if(autoEvent->WaitOne(TimeSpan(0, 0, 1), false))
{
Console::WriteLine(S"Work method signaled.");
}
else
{
Console::WriteLine(S"Timed out waiting for work "
S"method to signal.");
}
Console::WriteLine(S"Main ending.");
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
WaitHandle クラス | WaitHandle メンバ | System.Threading 名前空間 | WaitHandle.WaitOne オーバーロードの一覧