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); } }
private TaskResult PerformTask <T>(UndoableTaskBase <T> task, T argument) { CancellableTaskServiceEventArgs e = new CancellableTaskServiceEventArgs((ITask)task); this.OnExecuting(e); if (e.Cancel) { return(TaskResult.Cancelled); } this.globallyRedoableTasks.Clear(); this.globallyRepeatableTasks.AddLast((IInternalTask)task); this.globallyUndoableTasks.AddLast((IUndoableTask)task); TaskResult taskResult = task.PerformTask((object)argument, TaskMode.FirstTime); this.TrimIfRequired((object)null); this.OnExecuted(new TaskServiceEventArgs((ITask)task)); return(taskResult); }
public TaskResult PerformTask <T>(UndoableTaskBase <T> task, T argument, object ownerKey = null) { if (!this.Enable || this.IsUndoing) { return(TaskResult.NoEnable); } ArgumentValidator.AssertNotNull <UndoableTaskBase <T> >(task, "task"); if (ownerKey == null) { return(this.PerformTask <T>(task, argument)); } CancellableTaskServiceEventArgs e = new CancellableTaskServiceEventArgs((ITask)task) { OwnerKey = ownerKey }; this.OnExecuting(e); if (e.Cancel) { return(TaskResult.Cancelled); } this.redoableDictionary.Remove(ownerKey); TaskService.TaskCollection <IInternalTask> taskCollection1; if (!this.repeatableDictionary.TryGetValue(ownerKey, out taskCollection1)) { taskCollection1 = new TaskService.TaskCollection <IInternalTask>(); this.repeatableDictionary[ownerKey] = taskCollection1; } taskCollection1.AddLast((IInternalTask)task); TaskService.TaskCollection <IUndoableTask> taskCollection2; if (!this.undoableDictionary.TryGetValue(ownerKey, out taskCollection2)) { taskCollection2 = new TaskService.TaskCollection <IUndoableTask>(); this.undoableDictionary[ownerKey] = taskCollection2; } taskCollection2.AddLast((IUndoableTask)task); TaskResult taskResult = task.PerformTask((object)argument, TaskMode.FirstTime); this.TrimIfRequired(ownerKey); this.OnExecuted(new TaskServiceEventArgs((ITask)task)); return(taskResult); }