private void TryStartNewThread(IExecutionQueue executor) { BEGIN: if (_disposed) return; //Second check to ensure concurrency threshold would not be exceeded. var currentConcurrency = Interlocked.Increment(ref _currentConcurrency); if (currentConcurrency > _maxConcurrency) { currentConcurrency = Interlocked.Decrement(ref _currentConcurrency); if (currentConcurrency == 0) { goto BEGIN; } return; } ExecutionEnvelope envelope; if (executor == null) { envelope = TakeNextTask(); } else { envelope = executor.Dequeue(); } if (envelope == null) { Interlocked.Decrement(ref _currentConcurrency); return; } if (Logger.IsDebugEnabled) { Logger.Debug("Starting new execution thread."); } var executionInfo = new ExecutionInfo { CurrentTask = envelope }; var tplTask = new Task(PerformTasks, executionInfo); executionInfo.TakeFreeCell(_executionInfos, _threadNames); executionInfo.TplTask = tplTask; tplTask.Start(); }