public static TTask WithBusyIndicator <TTask>([NotNull] this TTask task, [NotNull] IViewModel viewModel,
                                                      object message = null, bool?handleException = null)
            where TTask : Task
        {
            Should.NotBeNull(task, nameof(task));
            Should.NotBeNull(viewModel, nameof(viewModel));
            if (handleException == null)
            {
                handleException = ApplicationSettings.HandleTaskExceptionBusyIndicator;
            }
            if (task.IsCompleted)
            {
                if (handleException.Value)
                {
                    ToolkitExtensions.TryHandleTaskException(task, viewModel, viewModel.GetIocContainer(true));
                }
                return(task);
            }
            var token = viewModel.BeginBusy(message);

            task.TryExecuteSynchronously(t =>
            {
                token.Dispose();
                if (handleException.Value)
                {
                    ToolkitExtensions.TryHandleTaskException(t, viewModel, viewModel.GetIocContainer(true));
                }
            });
            return(task);
        }
Exemplo n.º 2
0
        public static TTask WithBusyIndicator <TTask>([NotNull] this TTask task, [NotNull] IViewModel viewModel, object message = null)
            where TTask : Task
        {
            Should.NotBeNull(task, nameof(task));
            Should.NotBeNull(viewModel, nameof(viewModel));
            if (task.IsCompleted)
            {
                return(task);
            }
            var token = viewModel.BeginBusy(message);

#if NET4
            task.TryExecuteSynchronously(t => token.Dispose());
#else
            task.ContinueWith((t, o) => ((IBusyToken)o).Dispose(), token, TaskContinuationOptions.ExecuteSynchronously);
#endif
            return(task);
        }