ThreadRun method defined by the Producer looks like this:
Code:
// Producer.ThreadRun
public void ThreadRun()
{
int count = 0;
Random r = new Random();
while (!_syncEvents.ExitThreadEvent.WaitOne(0, false))
{
lock (((ICollection)_queue).SyncRoot)
{
while (_queue.Count < 20)
{
_queue.Enqueue(r.Next(0,100));
_syncEvents.NewItemEvent.Set();
count++;
}
}
}
Console.WriteLine("Producer thread: produced {0} items", count);
}
This method moves in a loop until the event "exit thread" is reported. The status of this event is tested with the method WaitOne using the property defined by the class ExitThreadEvent SyncEvent. In this case, the state of the event is verified without blocking the current thread as the first argument used WaitOne is zero, indicating that the method should return immediately. If WaitOne returns true, the event in question is currently reported. If this is the case, the method returns ThreadRun, which has the effect of completing the current thread that runs it.
Bookmarks