/// <summary>
        ///     Invokes a task and wrap it to busy indicator.
        /// </summary>
        /// <param name="task">The specified <see cref="Task" />.</param>
        /// <param name="viewModel">The specified <see cref="IViewModel" />.</param>
        /// <param name="message">The specified message for the <c>BusyMessage</c> property.</param>
        /// <param name="handleException">
        ///     Responsible for handling errors if true errors will be processed using the
        ///     <see cref="ITaskExceptionHandler" /> interface; otherwise false
        /// </param>
        public static TTask WithBusyIndicator <TTask>([NotNull] this TTask task, [NotNull] IViewModel viewModel,
                                                      object message = null, bool?handleException = null)
            where TTask : Task
        {
            Should.NotBeNull(task, "task");
            Should.NotBeNull(viewModel, "viewModel");
            if (handleException == null)
            {
                handleException = ApplicationSettings.HandleTaskExceptionBusyIndicator;
            }
            if (task.IsCompleted)
            {
                if (handleException.Value)
                {
                    ToolkitExtensions.TryHandleTaskException(task, viewModel, viewModel.GetIocContainer(true));
                }
                return(task);
            }
            Guid beginBusy = viewModel.BeginBusy(message);

            task.TryExecuteSynchronously(t =>
            {
                viewModel.EndBusy(beginBusy);
                if (handleException.Value)
                {
                    ToolkitExtensions.TryHandleTaskException(t, viewModel, viewModel.GetIocContainer(true));
                }
            });
            return(task);
        }
Esempio n. 2
0
        public override async Task Execute(object parameter = null)
        {
            try
            {
                _viewModel.Error = string.Empty;
                try
                {
                    await _viewModel.SetBusy();

                    if (BeforeExecute != null)
                    {
                        await BeforeExecute();
                    }
                    await base.Execute(parameter);
                }
                finally
                {
                    await _viewModel.EndBusy();
                }
                if (AfterExecuteSucceeded != null)
                {
                    await AfterExecuteSucceeded();
                }
            }
            catch (InvalidOperationException e)
            {
                _viewModel.Error = e.Message;
                if (AfterExecuteFailed != null)
                {
                    await AfterExecuteFailed();
                }
            }
        }