public TaskManager(int threadsNum) { _idleThreads = new List <WorkThread>(threadsNum); _workingThreads = new List <WorkThread>(threadsNum); for (int i = 0; i < threadsNum; i++) { var wt = new WorkThread(); wt.WorkDone += SubThreadWorkDoneHandler; _idleThreads.Add(wt); } }
public void AddTask(Action action) { lock (_locker) { if (_idleThreads.Count > 0) { WorkThread wt = _idleThreads[_idleThreads.Count - 1]; _idleThreads.Remove(wt); _workingThreads.Add(wt); wt.Do(action); } else { _actionsQueue.Enqueue(action); } } }
private void SubThreadWorkDoneHandler(WorkThread workThread) { if (_actionsQueue.TryDequeue(out Action result)) { workThread.Do(result); return; } lock (_locker) { _workingThreads.Remove(workThread); _idleThreads.Add(workThread); if (_actionsQueue.IsEmpty && _workingThreads.Count == 0) { TaskManagerWorkDone?.Invoke(); } } }
private void SubThreadWorkDoneHandler(WorkThread workThread) { if (_actionsQueue.TryDequeue(out Action result)) { workThread.Do(result); return; } lock (_locker) { _workingThreads.Remove(workThread); _idleThreads.Add(workThread); if (_actionsQueue.IsEmpty && _workingThreads.Count == 0) { //TaskManagerWorkDone?.Invoke();//добавил цикл по закрытию потоков foreach (var thrd in _idleThreads) { thrd.Terminate(); } TaskManagerWorkDone?.Invoke(); } } }