Replacing a While loop with AutoResetEvent for blocking a thread
Recently, I was finally able to make a separate thread work. Now I am
trying to master synchronization.
As far as I know, pausing a thread with ThreadName.Suspend() is not a good
idea. At fist I used a While loop to block the thread. Later I noticed
that this consumes resources, so now I am trying to replace the loop with
an AutoResetEvent.
Here is some code (tell me if you the full code):
private void combTester(object sender, EventArgs e)
//happens in a timer, because it manipulates the GUI
{
if (!timer2Block)
{
//stuff happens
genBlock = false;//unblocks the thread
timer2Block = true;//blocks itself
//debugging stuff happens
}
if (done)
timer2.Enabled = false;
}
private void combGenerator(int currEl, int begVal)
{
//setting a variable
for (int c = begVal; c <= currEl + totalCells - maxCells; c++)
{
while (genBlock && !abortTime)
{
if (abortTime)
return;
}
genBlock = true;//blocks itself
//some recursive stuff happens,
//because of which I was forced to use a thread instead of timers
}
}
I tried different places to put the Wait() and Set() methods, but both the
tread and the timer get blocked and I don't know how to debug the program.
So, how can I replace the While loop with AutoResetEvent?
No comments:
Post a Comment