Example #1
0
        private TaskResult PerformTask <T>(TaskBase <T> task, T argument)
        {
            CancellableTaskServiceEventArgs e = new CancellableTaskServiceEventArgs((ITask)task);

            this.OnExecuting(e);
            if (e.Cancel)
            {
                return(TaskResult.Cancelled);
            }
            this.globallyRedoableTasks.Clear();
            this.globallyUndoableTasks.Clear();
            this.globallyRepeatableTasks.AddLast((IInternalTask)task);
            TaskResult taskResult = task.PerformTask((object)argument, TaskMode.FirstTime);

            this.TrimIfRequired((object)null);
            this.OnExecuted(new TaskServiceEventArgs((ITask)task));
            return(taskResult);
        }
Example #2
0
        private static void ExecuteInParallel(Dictionary <TaskBase <T>, T> taskDictionary, TaskMode taskMode)
        {
            List <TaskBase <T> > performedTasks = new List <TaskBase <T> >();
            object           performedTasksLock = new object();
            List <Exception> exceptions         = new List <Exception>();
            object           exceptionsLock     = new object();
            Dictionary <KeyValuePair <TaskBase <T>, T>, AutoResetEvent> dictionary = taskDictionary.ToDictionary((KeyValuePair <TaskBase <T>, T> x) => x, (KeyValuePair <TaskBase <T>, T> x) => new AutoResetEvent(false));

            foreach (KeyValuePair <TaskBase <T>, T> current in taskDictionary)
            {
                AutoResetEvent autoResetEvent = dictionary[current];
                IInternalTask  task           = current.Key;
                TaskBase <T>   undoableTask   = current.Key;
                T arg = current.Value;
                ThreadPool.QueueUserWorkItem(delegate(object param0)
                {
                    try
                    {
                        task.PerformTask(arg, taskMode);
                        lock (performedTasksLock)
                        {
                            performedTasks.Add(undoableTask);
                        }
                    }
                    catch (Exception item)
                    {
                        lock (exceptionsLock)
                        {
                            exceptions.Add(item);
                        }
                    }
                    autoResetEvent.Set();
                });
            }
            foreach (AutoResetEvent current2 in dictionary.Values)
            {
                current2.WaitOne();
            }
            if (exceptions.Count > 0)
            {
                throw new CompositeException("Unable to undo tasks", exceptions);
            }
        }