/// <summary> /// Checks whether more work is required for the worker queues /// </summary> /// <param name="state">the unused object state</param> private void Moderate(object state) { bool reactivate = false; state.LocalOwner(state.ToString()); moderatorTimer.Change(Timeout.Infinite, Timeout.Infinite); try { if (!stopEvent.SafeWaitHandle.IsClosed && !stopEvent.WaitOne(10)) { reactivate = true; foreach (KeyValuePair <int, ConcurrentQueue <TaskContainer> > list in tasks) { if (filling[list.Key] && list.Value.Count > highTaskThreshold) { filling[list.Key] = false; } else if (!filling[list.Key] && list.Value.Count < lowTaskThreshold) { filling[list.Key] = true; } if (filling[list.Key]) { OnGetMoreTasks(list.Key); } } if (watchDog != null) { ITaskProcessor[] procs; lock (processors) { procs = processors.ToArray(); } foreach (var proc in procs) { watchDog.WatchProcessor(proc); } } } else { stoppedEvent.Set(); } } finally { if (reactivate) { moderatorTimer.Change(workerPollTime, workerPollTime); } state.LocalOwner(null); } }