I'm trying to work with ThreadPool.QueueUserWorkItem but it seems that if i'll run 2 of them, meaning:
ThreadPool.QueueUserWorkItem(new WaitCallback(x=>function A);
ThreadPool.QueueUserWorkItem(new WaitCallback(x=>function B);
It sometimes will be stuck for less than a second. Any ideas?
one of the calls is a game countdown timer:
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();
}
}
Are you starting many of those tasks, each of which block for a long time? According to the information given this might be the case.
This means that a lot of threads can be active at at a time. When you use the thread-pool above the minimum limits starting an additional thread will be throttled for (I believe) 500ms. This might be the delay you are seeing.
How to resolve that problem?
awaitit is quite easy to do that.SetMinThreads.