最近,我终于能够使一个单独的线程工作。现在我正在尝试掌握同步。
据我所知,暂停线程ThreadName.Suspend()
并不是一个好主意。首先,我使用While
循环来阻塞线程。后来我注意到这会消耗资源,所以现在我试图用AutoResetEvent
.
这是一些代码(告诉我是否完整的代码):
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
}
}
我尝试了不同的地方来放置Wait()
和Set()
方法,但是踏板和计时器都被阻塞了,我不知道如何调试程序。
那么,我怎样才能用 替换While
循环AutoResetEvent
?