Exemple #1
0
        private void PerformTasks(TasksState <T> tasksState)
        {
            T task;

            lock (tasksState)
            {
                if (!tasksState.TasksToDo.Any() && !tasksState.TasksInProgress.Any())
                {
                    return;
                }

                task = FindTaskToDo(tasksState);
            }

            if (task == null)
            {
                if (tasksState.TasksInProgress.Any())
                {
                    return;
                }

                throw new Exception("There are some tasks with dependencies that can't be satisfied.");
            }

            MoveTaskToInProgress(tasksState, task);
            Task.Run(() => PerformTasks(tasksState));

            PerformTaskAndTheTasksDependentOnIt(tasksState, task);
        }
Exemple #2
0
 private static void MoveTaskToDone(TasksState <T> tasksState, T task)
 {
     lock (tasksState)
     {
         tasksState.TasksInProgress.Remove(task);
         tasksState.DoneTasks.Add(task);
     }
 }
Exemple #3
0
        public void PerformTasks(List <T> tasks)
        {
            var tasksState = new TasksState <T>()
            {
                DoneTasks       = new HashSet <T>(),
                TasksToDo       = new List <T>(tasks),
                TasksInProgress = new HashSet <T>()
            };

            PerformTasks(tasksState);
        }
Exemple #4
0
        private T FindTaskToDo(TasksState <T> tasksState)
        {
            foreach (var task in tasksState.TasksToDo)
            {
                var dependencies = _getDependenciesFunction(task);
                if (dependencies.All(d => tasksState.DoneTasks.Contains(d)))
                {
                    return(task);
                }
            }

            return(null);
        }
Exemple #5
0
        private void PerformTaskAndTheTasksDependentOnIt(TasksState <T> tasksState, T task)
        {
            _performTaskFunction(task).ContinueWith(taskResult =>
            {
                if (!taskResult.Result)
                {
                    return;
                }

                MoveTaskToDone(tasksState, task);
                PerformTasks(tasksState);
            }).Wait();
        }