public void Stop(bool immediate) { // Hold the lock for as little time as possible int currentWorkItemCount; lock (this) { _cancellationTokenHelper.Cancel(); // dispatched onto a new thread currentWorkItemCount = _numExecutingWorkItems; } if (currentWorkItemCount == 0) { // There was no scheduled work, so we're done FinalShutdown(); } }