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 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; } } }