Пример #1
0
 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);
         }
     }
 }
Пример #2
0
        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();
                }
            }
        }