Example #1
0
        /// <summary>
        /// The execute.
        /// </summary>
        private void Execute()
        {
            IsRunning = true;

            lock (_syncObj)
            {
                IsCommitting = false;
            }

            try
            {
                bool aborted        = false;
                int  progress       = 0;
                int  total          = _tasks.Count;
                var  processedTasks = new Stack <ITask>();
                while (!aborted && _tasks.Count > 0)
                {
                    ITask task = _tasks.Peek();
                    try
                    {
                        Log.Debug("Executing task '{0}'. ", task.Name);
                        if (_progressNotifyableViewModel != null)
                        {
                            _progressNotifyableViewModel.UpdateStatus(progress++, total, task);
                        }
                        else
                        {
                            // TODO: Display smooth detailed progress using the PleasWaitService
// ReSharper disable AccessToModifiedClosure
                            _dispatcherService.Invoke(() => _pleaseWaitService.UpdateStatus(progress++, total, task.Name));
// ReSharper restore AccessToModifiedClosure
                        }

                        if (task.AutomaticallyDispatch)
                        {
                            _dispatcherService.Invoke(task.Execute);
                        }
                        else
                        {
                            task.Execute();
                        }

                        processedTasks.Push(_tasks.Dequeue());
                    }
                    catch (Exception e)
                    {
                        Log.Error(e);

                        var messageResult = MessageResult.None;
                        _dispatcherService.Invoke(() =>
                        {
                            messageResult = _messageService.Show(string.Format(TaskExecutionErrorMessagePattern, task.Name), "Error", MessageButton.YesNoCancel, MessageImage.Error);
                        });

                        switch (messageResult)
                        {
                        case MessageResult.Yes:
                            progress--;
                            break;

                        case MessageResult.No:
                            processedTasks.Push(_tasks.Dequeue());
                            break;

                        case MessageResult.Cancel:
                            processedTasks.Push(_tasks.Dequeue());
                            aborted = true;
                            break;
                        }
                    }
                }

                if (aborted)
                {
                    progress = processedTasks.Count;
                    while (processedTasks.Count > 0)
                    {
                        ITask task = processedTasks.Pop();
                        Log.Debug("Rolling back task '{0}'. ", task.Name);

                        try
                        {
                            task.Rollback();
                        }
                        catch (Exception e)
                        {
                            Log.Warning("Rollback of task '{0}' failed", task.Name);
                            Log.Error(e);
                        }
                        finally
                        {
                            if (_progressNotifyableViewModel != null)
                            {
                                _progressNotifyableViewModel.UpdateStatus(--progress, total, task);
                            }
                            else
                            {
                                _dispatcherService.Invoke(() => _pleaseWaitService.UpdateStatus(--progress, total, string.Format("Rollback '{0}'", task.Name)));
                            }
                        }
                    }
                }
            }
            finally
            {
                if (_pleaseWaitService != null)
                {
                    _dispatcherService.Invoke(() => _pleaseWaitService.Hide());
                }

                IsRunning = false;

                if (_progressNotifyableViewModel != null && CloseViewModelOnTerminated)
                {
                    _dispatcherService.Invoke(() => _progressNotifyableViewModel.CloseViewModel(null));
                }

                if (_completedCallback != null)
                {
                    _dispatcherService.Invoke(() => _completedCallback.Invoke());
                }
            }
        }
Example #2
0
        /// <summary>
        /// The execute.
        /// </summary>
        private void Execute()
        {
            IsRunning = true;

            lock (_syncObj)
            {
                IsCommitting = false;
            }

            try
            {
                bool aborted        = false;
                int  progress       = 0;
                int  total          = _tasks.Count;
                var  processedTasks = new Stack <ITask>();
                while (!aborted && _tasks.Count > 0)
                {
                    ITask task = _tasks.Peek();
                    try
                    {
                        Log.Debug("Executing task '{0}'. ", task.Name);
                        if (_progressNotifyableViewModel != null)
                        {
                            _progressNotifyableViewModel.UpdateStatus(progress++, total, task);
                        }
                        else
                        {
                            // TODO: Display smooth detailed progress using the PleasWaitService
// ReSharper disable AccessToModifiedClosure
                            _dispatcherService.BeginInvoke(() => _pleaseWaitService.UpdateStatus(progress++, total, task.Name));
// ReSharper restore AccessToModifiedClosure
                        }

                        if (task.AutomaticallyDispatch)
                        {
                            _dispatcherService.Invoke(task.Execute);
                        }
                        else
                        {
                            task.Execute();
                        }

                        processedTasks.Push(_tasks.Dequeue());
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);

                        if (!_exceptionService.HandleException(ex))
                        {
                            processedTasks.Push(_tasks.Dequeue());
                            aborted = true;
                        }
                        else
                        {
                            processedTasks.Push(_tasks.Dequeue());
                        }
                    }
                }

                if (aborted)
                {
                    progress = processedTasks.Count;
                    while (processedTasks.Count > 0)
                    {
                        ITask task = processedTasks.Pop();
                        Log.Debug("Rolling back task '{0}'. ", task.Name);

                        try
                        {
                            task.Rollback();
                        }
                        catch (Exception e)
                        {
                            Log.Warning("Rollback of task '{0}' failed", task.Name);
                            Log.Error(e);
                        }
                        finally
                        {
                            if (_progressNotifyableViewModel != null)
                            {
                                _progressNotifyableViewModel.UpdateStatus(--progress, total, task);
                            }
                            else
                            {
                                _dispatcherService.BeginInvoke(() => _pleaseWaitService.UpdateStatus(--progress, total, string.Format("Rollback '{0}'", task.Name)));
                            }
                        }
                    }
                }
            }
            finally
            {
                if (_pleaseWaitService != null)
                {
                    _dispatcherService.Invoke(() => _pleaseWaitService.Hide());
                }

                IsRunning = false;

                if (_progressNotifyableViewModel != null && CloseViewModelOnTerminated)
                {
                    _dispatcherService.Invoke(() => _progressNotifyableViewModel.CloseViewModel(null));
                }

                if (_completedCallback != null)
                {
                    _dispatcherService.Invoke(() => _completedCallback.Invoke());
                }
            }
        }
Example #3
0
        /// <summary>
        /// The execute.
        /// </summary>
        private void Execute()
        {
            IsRunning = true;

            lock (_syncObj)
            {
                IsCommitting = false;
            }

            IPleaseWaitService pleaseWaitService = null;

            if (_viewModelType == null)
            {
                pleaseWaitService = GetService <IPleaseWaitService>();
            }

            try
            {
                bool aborted = false, retry = false;
                int  progress       = 0;
                int  total          = _tasks.Count;
                var  processedTasks = new Stack <ITask>();
                while (!aborted && _tasks.Count > 0)
                {
                    if (!retry)
                    {
                        processedTasks.Push(_tasks.Dequeue());
                    }

                    retry = false;
                    ITask task = processedTasks.Peek();
                    try
                    {
                        Log.Debug("Executing task '{0}'. ", task.Name);
                        if (pleaseWaitService != null)
                        {
                            pleaseWaitService.UpdateStatus(++progress, total, task.Name);
                        }
                        else if (_progressNotifyableViewModel != null)
                        {
                            _progressNotifyableViewModel.UpdateStatus(progress++, total, task);
                        }

                        _dispatcherService.Invoke(task.Execute);
                    }
                    catch (Exception e)
                    {
                        Log.Error(e);

                        var messageResult = _messageService.Show(string.Format(TaskExecutionErrorMessagePattern, task.Name), "Error", MessageButton.YesNoCancel, MessageImage.Error);
                        switch (messageResult)
                        {
                        case MessageResult.Yes:
                            retry = true;
                            break;

                        case MessageResult.Cancel:
                            aborted = true;
                            break;
                        }
                    }
                }

                if (aborted)
                {
                    while (processedTasks.Count > 0)
                    {
                        ITask task = processedTasks.Pop();
                        Log.Debug("Rolling back task '{0}'. ", task.Name);

                        try
                        {
                            if (pleaseWaitService != null)
                            {
                                pleaseWaitService.UpdateStatus(progress--, total, string.Format("Rollback '{0}'", task.Name));
                            }
                            else if (_progressNotifyableViewModel != null)
                            {
                                _progressNotifyableViewModel.UpdateStatus(progress--, total, task);
                            }

                            task.Rollback();
                        }
                        catch (Exception e)
                        {
                            Log.Warning("Rollback of task '{0}' failed", task.Name);
                            Log.Error(e);
                        }
                    }
                }
            }
            finally
            {
                if (pleaseWaitService != null)
                {
                    pleaseWaitService.Hide();
                }

                IsRunning = false;

                _dispatcherService.Invoke(() =>
                {
                    if (_progressNotifyableViewModel != null)
                    {
                        _progressNotifyableViewModel.CloseViewModel(null);
                    }
                });

                if (_completedCallback != null)
                {
                    _dispatcherService.Invoke(() => _completedCallback.Invoke());
                }
            }
        }