void QueueAction(T next) { var ct = CancelTokenSrc.Token; Task task = new Task(() => { // If we are cancelled before we start, push the job back on the queue so it can be dealt with by another runner. if (ct.IsCancellationRequested) { Queue.Enqueue(next); return; } SafeAction(next); }, ct); task.ContinueWith((t) => { byte val; RunningTasks.TryRemove(t, out val); WaitForTaskEvent.Set( ); }, ct); RunningTasks.TryAdd(task, 0); task.Start( ); }
/// <summary> /// Stop the Actioner /// </summary> /// <param name="timeoutMs">How long to wait in ms for the actioner to complete, -1 to wait forever</param> /// <returns>true if thread was or is stopped.</returns> public bool Stop(int timeoutMs = -1) { if (State != ActionerState.Stopped) { lock (_syncRoot) { if (State == ActionerState.Running || State == ActionerState.WaitingForQueue || State == ActionerState.WaitingForTask) { State = ActionerState.Stopping; } } // cancel running tasks CancelTokenSrc?.Cancel(); } // Get rid of the looping thread if (LoopingThread != null) { WaitForQueueEvent.Set(); WaitForTaskEvent.Set(); if (LoopingThread.Join(timeoutMs)) { LoopingThread = null; return(true); } return(false); } return(true); }