/// <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); }
/// <summary> /// Start the actioner. It will begin processing the entries until stopped. /// </summary> public void Start() { CancelTokenSrc?.Dispose( ); CancelTokenSrc = new CancellationTokenSource(); if (State == ActionerState.Stopping) { throw new ActionerStartingWhileStoppingException(Queue.Name ?? "[Unnamed]"); } if (State == ActionerState.Running || State == ActionerState.WaitingForQueue || State == ActionerState.WaitingForTask) { return; } if (LoopingThread != null) { throw new ApplicationException("Attempted to start a loop while another was still running. This should never happen."); } State = ActionerState.Running; LoopingThread = new Thread(ActionLoop) { Name = $"{ActionLoopTheadPrefix} {Queue.Name}", IsBackground = true }; LoopingThread.Start(); }
private bool _disposedValue; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!_disposedValue) { if (disposing) { Stop(DisposeStopWait); CancelTokenSrc?.Dispose( ); } _disposedValue = true; } }