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); } }
public bool StartUserWorkItem(WaitCallback callBack, object parameter, TimeSpan timeout) { CustomThreadPoolThread thread = GetNextIdleThread(timeout); if (thread != null) { thread.ExecuteUserWorkItem(callBack, parameter); } return(thread != null); }
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; }