public void Start(ThreadActor actor, CancellationToken cancellationToken, string name = null) { cancellationToken.Register(this.Kill); name = name ?? $"Thread worker thread #{kDefaultThreadNameCounter++}"; // Reset this.Kill(); // Reset graceful shutdown indicators m_finishedGracefully = false; m_gracefulShutdownIndicator = new TaskCompletionSource <bool>(); // Create new data m_activeThreadData = new ThreadWorkerDataTracker <TInput, TExecutionState, TOutput>(this.InputToProcess, this.ExecutionState, this.OutputResults); m_thread = new Thread(new ParameterizedThreadStart(_Runner)); m_thread.Name = name; m_thread.IsBackground = true; m_thread.Start(m_activeThreadData); this.IsActiveChanged?.Invoke(this, EventArgs.Empty); void _Runner(object state) { actor((ThreadWorkerDataTracker <TInput, TExecutionState, TOutput>)state); m_finishedGracefully = true; var gracefulShutdownNotifier = m_gracefulShutdownIndicator; this.DeactivateAndCleanup(); gracefulShutdownNotifier.TrySetResult(true); } }
// ==================[ Methods ]============================ public void Start(ThreadActor actor, string name = null) { this.Start(actor, CancellationToken.None, name); }