/// <summary> /// Starts this instance of <see cref="ParallelRunnerController"/>. /// This method is indempotent /// </summary> /// <param name="token">The cancellation token</param> public void Start(CancellationToken token) { WaitHandle[] waitHandles; lock (_sync) { if (_workers[0] != null) { return; } for (int i = 0; i < _threadCount; i++) { var worker = new ParallelRunnerWorker(this, _processQueue); worker.Start(token); _workers[i] = worker; } waitHandles = _workers.Select(x => x.WaitHandle).ToArray(); } Task.Run(() => { WaitHandle.WaitAll(waitHandles); _waitHandle.Set(); foreach (var worker in _workers) { worker.Dispose(); } }, CancellationToken.None); _processQueueThread = new Thread(() => ProcessHoldQueue(token)); _processQueueThread.Start(); }
/// <summary> /// Starts this instance of <see cref="ParallelRunnerController"/>. /// This method is indempotent /// </summary> /// <param name="token">The cancellation token</param> public void Start(CancellationToken token) { WaitHandle[] waitHandles; lock (_sync) { if (_workers[0] != null) { return; } for (int i = 0; i < _threadCount; i++) { var worker = new ParallelRunnerWorker(token, _processQueue); worker.Start(); _workers[i] = worker; } waitHandles = _workers.Select(x => x.WaitHandle).ToArray(); } Task.Run(() => { WaitHandle.WaitAll(waitHandles); lock (_sync) { // if the handle is already closed means we already were disposed // and so were the workers if (_waitHandle != null && !_waitHandle.SafeWaitHandle.IsClosed) { for (int i = 0; i < _threadCount; i++) { _workers[i].DisposeSafely(); _workers[i] = null; } _waitHandle.Set(); } } }, CancellationToken.None); _processQueueThread = new Thread(() => ProcessHoldQueue(token)) { IsBackground = true }; _processQueueThread.Start(); }
/// <summary> /// Starts this instance of <see cref="ParallelRunnerController"/>. /// This method is indempotent /// </summary> /// <param name="token">The cancellation token</param> public void Start(CancellationToken token) { WaitHandle[] waitHandles; lock (_sync) { if (_workers[0] != null) return; for (int i = 0; i < _threadCount; i++) { var worker = new ParallelRunnerWorker(this, _processQueue); worker.Start(token); _workers[i] = worker; } waitHandles = _workers.Select(x => x.WaitHandle).ToArray(); } Task.Run(() => { WaitHandle.WaitAll(waitHandles); _waitHandle.Set(); foreach (var worker in _workers) { worker.Dispose(); } }, CancellationToken.None); _processQueueThread = new Thread(() => ProcessHoldQueue(token)); _processQueueThread.Start(); }