我正在尝试使用 ThreadPool.QueueUserWorkItem 但似乎如果我要运行其中的 2 个,这意味着:
ThreadPool.QueueUserWorkItem(new WaitCallback(x=>function A);
ThreadPool.QueueUserWorkItem(new WaitCallback(x=>function B);
它有时会卡住不到一秒钟。有任何想法吗?
其中一个调用是游戏倒计时:
ThreadPool.QueueUserWorkItem(new WaitCallback(x=>initClock(0,0)));
private void initClock(int sec , int hunS)
{
int half = gameClock / 2;
seconds = sec;
while (true)
{
while (clockLock == false && seconds < gameClock)
{
hunSec = hunS;
while (clockLock == false && hunSec < 100)
{
Thread.Sleep(10);
updateClock(seconds, hunSec);
hunSec++;
}
seconds++;
if (half == seconds)
{
panel5.BackColor = Color.Red;
}
}
}
}
private void updateClock(int sec, int secRem)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<int, int>(updateClock), sec, secRem);
}
else
{
clock_Label.Text = sec.ToString() + ':' + secRem.ToString();
}
}