예제 #1
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);
        }
예제 #2
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);
        }
예제 #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);
     }
 }
예제 #4
0
        private void TaskCompleted(IInternalTask task)
        {
            lock (tasksMutex)
            {
                workingTasks.Remove(task);
                CheckQueue();

                if (queue.Count == 0 && workingTasks.Count == 0)
                {
                    OnCompleted();
                }
            }
        }
예제 #5
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);
        }
예제 #6
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);
            }
        }
예제 #7
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;
                }
            }
        }
예제 #8
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);
        }
예제 #9
0
        private TaskResult Undo()
        {
            if (this.globallyRepeatableTasks.Count < 1)
            {
                throw new InvalidOperationException("No task to undo.");
            }
            IUndoableTask undoableTask        = this.globallyUndoableTasks.Pop();
            IInternalTask internalTask        = this.globallyRepeatableTasks.Pop();
            CancellableTaskServiceEventArgs e = new CancellableTaskServiceEventArgs((ITask)undoableTask);

            this.OnUndoing(e);
            if (e.Cancel)
            {
                this.globallyUndoableTasks.AddLast(undoableTask);
                this.globallyRepeatableTasks.AddLast(internalTask);
                return(TaskResult.Cancelled);
            }
            this.globallyRedoableTasks.AddLast(undoableTask);
            TaskResult taskResult = undoableTask.Undo();

            this.OnUndone(new TaskServiceEventArgs((ITask)undoableTask));
            return(taskResult);
        }
예제 #10
0
 void IInternalTaskService.NotifyTaskRepeatableChanged(IInternalTask task)
 {
 }
예제 #11
0
        /// <summary>
        ///     Starts the execution of a task.
        /// </summary>
        /// <param name="task">The task that should be executed.</param>
        private async void StartTask(IInternalTask task)
        {
            await task.Execute();

            TaskCompleted(task);
        }