private CustomThreadPoolThread GetNextIdleThread(TimeSpan timeout)
{
if (_semaphore.WaitOne(timeout))
{
lock (_threads)
{
if (_aborting)
return null;
CustomThreadPoolThread thread = null;
foreach (var item in _threads)
if (item.IsIdle)
{
thread = item;
break;
}
if (thread == null)
{
Debug.Assert(_threads.Count <= _threadPoolSize);
_lastThreadIndex++;
thread = new CustomThreadPoolThread(Name + _lastThreadIndex.ToString(), Priority);
thread.Idle += OnIdle;
_threads.Add(thread);
}
return thread;
}
}
else
return null;
}