private void Registry_StateUpdate(int id, TaskRunnerState state) { if (id == Id) { UpdateState(state); } }
ThreadProc() { // Change state to indicate that the task runner is started. __state = TaskRunnerState.Running; // Emit an event to notify listeners that the task runner started up. if (Started != null) { Started(); } // Continue looping until Stop() is called and all critical tasks are finished. while (!IsStopping || IsStopping && HasEnqueuedCriticalTasks) { // Most of this is only important if something is in the queue. if (RunningTasks < ThreadPoolMaxThreads && __queue.Peek() != null) { // Execute the task on the thread pool. PriorityValuePair <Task> elem = __queue.Dequeue(); Task task = elem.Value; if (task != null) { // We're passing a null context here because the task is packaged with its // own execution context that will be inserted during Task.Run(). ThreadPool.QueueUserWorkItem((x) => { task.Run(); }, null); } // Mark the task as starting execution. // Note: This should run even if the task is null above, otherwise the counts // for the number of running/waiting tasks will become skewed. onRunningTask(elem); } else { // Yield the rest of the time slice. Thread.Sleep(1); } } // Wait for all critical tasks to finish running before stopping. while (HasWaitingCriticalTasks) { Thread.Sleep(1); } // Flag the task runner as stopped. __state = TaskRunnerState.Stopped; __thread = null; // Emit an event to notify listeners that the task runner stopped. if (Stopped != null) { Stopped(); } }
Stop() { if (!IsRunning) { // If the task runner isn't Stopped, we don't want to maniuplate the state. That // would be an error. throw new InvalidOperationException("Cannot stop a task runner that is not running."); } // Transition to Stopping state to cause threadProc() to wind down. __state = TaskRunnerState.Stopping; }
ConcurrentPriorityTaskRunner() { __queue = new ConcurrentPriorityQueue <Task>(); __state = TaskRunnerState.Stopped; __thread = null; __runningCriticalTasks = 0; __runningNoncriticalTasks = 0; __waitingCriticalTasks = 0; __waitingNoncriticalTasks = 0; __runningCriticalTasksLock = new object(); __runningNoncriticalTasksLock = new object(); __waitingCriticalTasksLock = new object(); __waitingNoncriticalTasksLock = new object(); }
Start() { if (!IsStopped) { // If the task runner isn't Stopped, we don't want to launch another thread and // maniuplate the state unnecessarily. That would be an error. throw new InvalidOperationException("Cannot start a task runner that is not stopped."); } // Set the state to Starting. __state = TaskRunnerState.Starting; // Launch a thread to handle executing tasks on the thread pool. __thread = new Thread(ThreadProc); __thread.Start(); }
protected bool UpdateState(TaskRunnerState state) => StateSubject.Post(state, true);
protected override async Task SignalStateUpdated(int id, TaskRunnerState state) { await base.SignalStateUpdated(id, state).ConfigureAwait(false); await _context.Clients.All.StateUpdated(id, state).ConfigureAwait(false); }
protected virtual Task SignalStateUpdated(int id, TaskRunnerState state) { StateUpdated?.Invoke(id, state); return(Task.CompletedTask); }