I have queue maintained by a single thread. The thread will do some operations on each item of the queue in turn. I want the single thread could skip certain queue item and go on for the next item when I click a button (or some other operation).
Actually I have already had a solution which uses two threads, one used to maintain the queue, the other used to do operations on each queue item. But this causes some trouble when I pause or skip the whole process in real situation. The communication between the two threads seems not working well.
Let's say that the queue has items 1, 2, 3, 4, 5. Assuming that the thread is working on item 1 now. When I click the button, the thread should stop current operation and skip all scheduled operations on item 1 and then work on item 2. Operations on each item is almost the same. Like making dishes, when I am cooking the first dish, I received an command to ignore it, I must throw the first dish away and start cooking the second dish.
I wonder whether it is possible to use a single thread in my scenario?
The code used now is like the following snippet.
ManualResetEvent _rstEvent = new ManualResetEvent(false);
TestItem _currentTest;
public void FuncMaintainTestQueue()
{
while(TestQueue.Count > 0)
{
_currentTest = TestQueue.Deque();
Thread thread = new Thread(FuncDoTest);
thread.Start();
_rstEvent.WaitOne();
}
}
private void FuncDoTest()
{
//Do test operations on _currentTest
...
//Finished current test item
_rstEvent.Set();
}
//Outside caller
Thread _thread1 = new Thread(FuncMaintainTestQueue);
thread1.Start();