I have two threads, one thread processes the queue, and the other thread adds material to the queue.
- I want the queue processing flow to sleep when finished processing the queue
- I want the second thread to say that he woke up when he added the item to the queue
However, these functions are called System.Threading.SynchronizationLockException: Object synchronization method was called from an unsynchronized block of codewhen called Monitor.PulseAll(waiting);, because I did not synchronize the function with the waiting object. [which I do not want to do, I want to be able to handle when adding items to the queue]. How can I achieve this?
Queue<object> items = new Queue<object>();
object waiting = new object();
1st stream
public void ProcessQueue()
{
while (true)
{
if (items.Count == 0)
Monitor.Wait(waiting);
object real = null;
lock(items) {
object item = items.Dequeue();
real = item;
}
if(real == null)
continue;
.. bla bla bla
}
}
The second topic includes
public void AddItem(object o)
{
... bla bla bla
lock(items)
{
items.Enqueue(o);
}
Monitor.PulseAll(waiting);
}
Kurru source
share