Esempio n. 1
0
        private TaskResult Repeat()
        {
            IInternalTask internalTask = this.globallyRepeatableTasks.Peek();

            if (!internalTask.Repeatable)
            {
                return(TaskResult.NoTask);
            }
            CancellableTaskServiceEventArgs e = new CancellableTaskServiceEventArgs((ITask)internalTask);

            this.OnExecuting(e);
            if (e.Cancel)
            {
                return(TaskResult.Cancelled);
            }
            this.globallyRedoableTasks.Clear();
            this.globallyRepeatableTasks.AddLast(internalTask);
            IUndoableTask undoableTask = internalTask as IUndoableTask;

            if (undoableTask != null)
            {
                this.globallyUndoableTasks.AddLast(undoableTask);
            }
            else
            {
                this.globallyUndoableTasks.Clear();
                this.globallyRedoableTasks.Clear();
            }
            TaskResult taskResult = internalTask.PerformTask(internalTask.Argument, TaskMode.Repeat);

            this.OnExecuted(new TaskServiceEventArgs((ITask)internalTask));
            return(taskResult);
        }
Esempio n. 2
0
        private TaskResult Redo()
        {
            if (this.globallyRedoableTasks.Count < 1)
            {
                throw new InvalidOperationException("No task to redo.");
            }
            IUndoableTask undoableTask        = this.globallyRedoableTasks.Pop();
            CancellableTaskServiceEventArgs e = new CancellableTaskServiceEventArgs((ITask)undoableTask);

            this.OnRedoing(e);
            if (e.Cancel)
            {
                this.globallyRedoableTasks.AddLast(undoableTask);
                return(TaskResult.Cancelled);
            }
            IInternalTask internalTask = (IInternalTask)undoableTask;

            this.globallyRepeatableTasks.AddLast(internalTask);
            this.globallyUndoableTasks.AddLast(undoableTask);
            TaskResult taskResult = internalTask.PerformTask(internalTask.Argument, TaskMode.Redo);

            this.TrimIfRequired((object)null);
            this.OnRedone(new TaskServiceEventArgs((ITask)undoableTask));
            return(taskResult);
        }
Esempio n. 3
0
 private static void ExecuteInSequence(Dictionary <TaskBase <T>, T> taskDictionary, TaskMode taskMode)
 {
     foreach (KeyValuePair <TaskBase <T>, T> current in taskDictionary)
     {
         IInternalTask key = current.Key;
         key.PerformTask(current.Value, taskMode);
     }
 }
Esempio n. 4
0
        public TaskResult Repeat(object ownerKey = null)
        {
            if (ownerKey == null)
            {
                return(this.Repeat());
            }
            TaskService.TaskCollection <IInternalTask> taskCollection1;
            if (!this.repeatableDictionary.TryGetValue(ownerKey, out taskCollection1))
            {
                throw new InvalidOperationException("No tasks to be redone for the specified owner key.");
            }
            IInternalTask internalTask = taskCollection1.Peek();

            if (!internalTask.Repeatable)
            {
                return(TaskResult.NoTask);
            }
            CancellableTaskServiceEventArgs e = new CancellableTaskServiceEventArgs((ITask)internalTask)
            {
                OwnerKey = ownerKey
            };

            this.OnExecuting(e);
            if (e.Cancel)
            {
                return(TaskResult.Cancelled);
            }
            taskCollection1.AddLast(internalTask);
            TaskService.TaskCollection <IUndoableTask> taskCollection2;
            if (!this.undoableDictionary.TryGetValue(ownerKey, out taskCollection2))
            {
                taskCollection2 = new TaskService.TaskCollection <IUndoableTask>();
                this.undoableDictionary[ownerKey] = taskCollection2;
            }
            IUndoableTask undoableTask = internalTask as IUndoableTask;

            if (undoableTask != null)
            {
                taskCollection2.AddLast(undoableTask);
            }
            else
            {
                this.undoableDictionary[ownerKey] = (TaskService.TaskCollection <IUndoableTask>)null;
                this.redoableDictionary[ownerKey] = (TaskService.TaskCollection <IUndoableTask>)null;
            }
            TaskResult taskResult = internalTask.PerformTask(internalTask.Argument, TaskMode.Repeat);

            this.TrimIfRequired(ownerKey);
            this.OnExecuted(new TaskServiceEventArgs((ITask)internalTask));
            return(taskResult);
        }
Esempio n. 5
0
        private static void ExecuteInParallel(Dictionary <UndoableTaskBase <T>, T> taskDictionary, TaskMode taskMode)
        {
            List <UndoableTaskBase <T> > performedTasks = new List <UndoableTaskBase <T> >();
            object           performedTasksLock         = new object();
            List <Exception> exceptions     = new List <Exception>();
            object           exceptionsLock = new object();
            Dictionary <KeyValuePair <UndoableTaskBase <T>, T>, AutoResetEvent> dictionary = taskDictionary.ToDictionary((KeyValuePair <UndoableTaskBase <T>, T> x) => x, (KeyValuePair <UndoableTaskBase <T>, T> x) => new AutoResetEvent(false));

            foreach (KeyValuePair <UndoableTaskBase <T>, T> current in taskDictionary)
            {
                AutoResetEvent       autoResetEvent = dictionary[current];
                IInternalTask        task           = current.Key;
                UndoableTaskBase <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)
            {
                CompositeUndoableTask <T> .SafelyUndoTasks(performedTasks.Cast <IUndoableTask>());

                throw new CompositeException("Unable to undo tasks", exceptions);
            }
        }
Esempio n. 6
0
        private static void ExecuteSequentially(Dictionary <UndoableTaskBase <T>, T> taskDictionary, TaskMode taskMode)
        {
            List <UndoableTaskBase <T> > list = new List <UndoableTaskBase <T> >();

            foreach (KeyValuePair <UndoableTaskBase <T>, T> current in taskDictionary)
            {
                IInternalTask key = current.Key;
                try
                {
                    key.PerformTask(current.Value, taskMode);
                    list.Add(current.Key);
                }
                catch (Exception)
                {
                    CompositeUndoableTask <T> .SafelyUndoTasks(list.Cast <IUndoableTask>());

                    throw;
                }
            }
        }
Esempio n. 7
0
        public TaskResult Redo(object ownerKey = null)
        {
            if (ownerKey == null)
            {
                return(this.Redo());
            }
            TaskService.TaskCollection <IUndoableTask> taskCollection1;
            if (!this.redoableDictionary.TryGetValue(ownerKey, out taskCollection1))
            {
                throw new InvalidOperationException("No tasks to be redone for the specified owner key.");
            }
            IUndoableTask undoableTask        = taskCollection1.Pop();
            CancellableTaskServiceEventArgs e = new CancellableTaskServiceEventArgs((ITask)undoableTask);

            this.OnRedoing(e);
            if (e.Cancel)
            {
                taskCollection1.AddLast(undoableTask);
                return(TaskResult.Cancelled);
            }
            IInternalTask internalTask = (IInternalTask)undoableTask;

            TaskService.TaskCollection <IInternalTask> taskCollection2;
            if (!this.repeatableDictionary.TryGetValue(ownerKey, out taskCollection2))
            {
                taskCollection2 = new TaskService.TaskCollection <IInternalTask>();
            }
            taskCollection2.AddLast(internalTask);
            TaskService.TaskCollection <IUndoableTask> taskCollection3;
            if (!this.undoableDictionary.TryGetValue(ownerKey, out taskCollection3))
            {
                taskCollection3 = new TaskService.TaskCollection <IUndoableTask>();
            }
            taskCollection3.AddLast(undoableTask);
            TaskResult taskResult = internalTask.PerformTask(internalTask.Argument, TaskMode.Redo);

            this.TrimIfRequired(ownerKey);
            this.OnRedone(new TaskServiceEventArgs((ITask)undoableTask));
            return(taskResult);
        }