オブジェクトのロックを解放し、現在のスレッドがロックを再取得するまでそのスレッドをブロックします。
オーバーロードの一覧
オブジェクトのロックを解放し、現在のスレッドがロックを再取得するまでそのスレッドをブロックします。
[Visual Basic] Overloads Public Shared Function Wait(Object) As Boolean
オブジェクトのロックを解放し、現在のスレッドがロックを再取得するまで、または指定した時間が経過するまでそのスレッドをブロックします。
[Visual Basic] Overloads Public Shared Function Wait(Object, Integer) As Boolean
[JScript] public static function Wait(Object, int) : Boolean;
オブジェクトのロックを解放し、現在のスレッドがロックを再取得するまで、または指定した時間が経過するまでそのスレッドをブロックします。
[Visual Basic] Overloads Public Shared Function Wait(Object, TimeSpan) As Boolean
[JScript] public static function Wait(Object, TimeSpan) : Boolean;
Pulse メソッドまたは PulseAll メソッドを呼び出したオブジェクトからの通知が行われるまで、または指定した時間が経過するまで待機します。このメソッドは、コンテキストの同期ドメイン (同期されたコンテキストの場合) が待機の前に終了し、再取得されるかどうかも指定します。
[Visual Basic] Overloads Public Shared Function Wait(Object, Integer, Boolean) As Boolean
[JScript] public static function Wait(Object, int, Boolean) : Boolean;
オブジェクトのロックを解放し、現在のスレッドがロックを再取得するまで、または指定した時間が経過するまで、またはオプションで待機の前に同期コンテキストの同期ドメインを終了してドメインを再取得するまで、そのスレッドをブロックします。
[Visual Basic] Overloads Public Shared Function Wait(Object, TimeSpan, Boolean) As Boolean
[JScript] public static function Wait(Object, TimeSpan, Boolean) : Boolean;
使用例
[Visual Basic, C#, C++] Wait メソッドを使用する方法を次のコード例に示します。
[Visual Basic, C#, C++] メモ ここでは、Wait のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
Imports System
Imports System.Threading
Imports System.Collections
Namespace MonitorCS1
Class MonitorSample
Private MAX_LOOP_TIME As Integer = 1000
Private m_smplQueue As Queue
Public Sub New()
m_smplQueue = New Queue()
End Sub 'New
Public Sub FirstThread()
Dim counter As Integer = 0
SyncLock m_smplQueue
While counter < MAX_LOOP_TIME
'Wait, if the queue is busy.
Monitor.Wait(m_smplQueue)
'Push one element.
m_smplQueue.Enqueue(counter)
'Release the waiting thread.
Monitor.Pulse(m_smplQueue)
counter += 1
End While
End SyncLock
End Sub 'FirstThread
Public Sub SecondThread()
SyncLock m_smplQueue
'Release the waiting thread.
Monitor.Pulse(m_smplQueue)
'Wait in the loop while the queue is busy.
'Exit on the time-out when the first thread stops.
While Monitor.Wait(m_smplQueue, 1000)
'Pop the first element.
Dim counter As Integer = CInt(m_smplQueue.Dequeue())
'Print the first element.
Console.WriteLine(counter.ToString())
'Release the waiting thread.
Monitor.Pulse(m_smplQueue)
End While
End SyncLock
End Sub 'SecondThread
'Return the number of queue elements.
Public Function GetQueueCount() As Integer
Return m_smplQueue.Count
End Function 'GetQueueCount
Public Shared Sub Main(args() As String)
'Create the MonitorSample object.
Dim test As New MonitorSample()
'Create the first thread.
Dim tFirst As New Thread(AddressOf test.FirstThread)
'Create the second thread.
Dim tSecond As New Thread(AddressOf test.SecondThread)
'Start threads.
tFirst.Start()
tSecond.Start()
'wait to the end of the two threads
tFirst.Join()
tSecond.Join()
'Print the number of queue elements.
Console.WriteLine(("Queue Count = " + test.GetQueueCount().ToString()))
End Sub 'Main
End Class 'MonitorSample
End Namespace 'MonitorCS1
[C#]
using System;
using System.Threading;
using System.Collections;
namespace MonitorCS1
{
class MonitorSample
{
const int MAX_LOOP_TIME = 1000;
Queue m_smplQueue;
public MonitorSample()
{
m_smplQueue = new Queue();
}
public void FirstThread()
{
int counter = 0;
lock(m_smplQueue)
{
while(counter < MAX_LOOP_TIME)
{
//Wait, if the queue is busy.
Monitor.Wait(m_smplQueue);
//Push one element.
m_smplQueue.Enqueue(counter);
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);
counter++;
}
}
}
public void SecondThread()
{
lock(m_smplQueue)
{
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);
//Wait in the loop, while the queue is busy.
//Exit on the time-out when the first thread stops.
while(Monitor.Wait(m_smplQueue,1000))
{
//Pop the first element.
int counter = (int)m_smplQueue.Dequeue();
//Print the first element.
Console.WriteLine(counter.ToString());
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);
}
}
}
//Return the number of queue elements.
public int GetQueueCount()
{
return m_smplQueue.Count;
}
static void Main(string[] args)
{
//Create the MonitorSample object.
MonitorSample test = new MonitorSample();
//Create the first thread.
Thread tFirst = new Thread(new ThreadStart(test.FirstThread));
//Create the second thread.
Thread tSecond = new Thread(new ThreadStart(test.SecondThread));
//Start threads.
tFirst.Start();
tSecond.Start();
//wait to the end of the two threads
tFirst.Join();
tSecond.Join();
//Print the number of queue elements.
Console.WriteLine("Queue Count = " + test.GetQueueCount().ToString());
}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
using namespace System::Collections;
__gc class MonitorSample
{
public:
MonitorSample();
void FirstThread();
void SecondThread();
int GetQueueCount();
private:
static const int MAX_LOOP_TIME = 1000;
Queue* m_smplQueue;
};
MonitorSample::MonitorSample()
{
m_smplQueue = new Queue();
}
void MonitorSample::FirstThread()
{
int counter = 0;
Monitor::Enter(m_smplQueue);
while(counter < MAX_LOOP_TIME)
{
//Wait, if the queue is busy.
Monitor::Wait(m_smplQueue);
//Push one element.
m_smplQueue->Enqueue(__box(counter));
//Release the waiting thread.
Monitor::Pulse(m_smplQueue);
counter++;
}
Monitor::Exit(m_smplQueue);
}
void MonitorSample::SecondThread()
{
Monitor::Enter(m_smplQueue);
//Release the waiting thread.
Monitor::Pulse(m_smplQueue);
//Wait in the loop, while the queue is busy.
//Exit on the time-out when the first thread stopped.
while(Monitor::Wait(m_smplQueue,1000))
{
//Pop the first element.
int counter = *dynamic_cast<__box int*>(m_smplQueue->Dequeue());
//Print the first element.
Console::WriteLine(counter.ToString());
//Release the waiting thread.
Monitor::Pulse(m_smplQueue);
}
Monitor::Exit(m_smplQueue);
}
//Return the number of queue elements.
int MonitorSample::GetQueueCount()
{
return m_smplQueue->Count;
}
int main()
{
//Create the MonitorSample object.
MonitorSample* test = new MonitorSample();
//Create the first thread.
Thread* tFirst = new Thread(new ThreadStart(test, &MonitorSample::FirstThread));
//Create the second thread.
Thread* tSecond = new Thread(new ThreadStart(test, &MonitorSample::SecondThread));
//Start threads.
tFirst->Start();
tSecond->Start();
//wait to the end of the two threads
tFirst->Join();
tSecond->Join();
//Print the number of queue elements.
Console::WriteLine(String::Concat(S"Queue Count = ", test->GetQueueCount().ToString()));
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。