public async Task <ProgressResult <T> > Do <T>(Func <ProgressDialogController, T> function, string message, bool canCancel, bool throwErrors)
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var controller = await DialogCoordinator.Instance.ShowProgressAsync(dialogContext, "Please Wait", message, canCancel);

            controller.SetIndeterminate();

            var controllerCancel = Task.Factory.StartNew(delegate
            {
                while (true)
                {
                    if (!controller.IsOpen)
                    {
                        break;
                    }

                    if (controller.IsCanceled)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
            }, cancellationTokenSource.Token);

            try
            {
                var functionTask = Task.Factory.StartNew(() => function.Invoke(controller), cancellationTokenSource.Token);

                ProgressResult <T> result;
                if (await Task.WhenAny(functionTask, controllerCancel) == controllerCancel)
                {
                    cancellationTokenSource.Cancel();
                    result = ProgressResult.GetCancelled <T>();
                }
                else
                {
                    if (functionTask.Status == TaskStatus.RanToCompletion)
                    {
                        result = ProgressResult.GetSuccess(functionTask.Result);
                    }
                    else
                    {
                        result = ProgressResult.GetErrored <T>();

                        if (throwErrors && functionTask.Exception != null)
                        {
                            ExceptionDispatchInfo.Capture(functionTask.Exception.InnerException).Throw();
                        }
                    }
                }

                return(result);
            }
            catch (Exception)
            {
                if (throwErrors)
                {
                    throw;
                }

                return(ProgressResult.GetErrored <T>());
            }
            finally
            {
                cancellationTokenSource.Cancel();

                if (controller != null)
                {
                    await controller.CloseAsync();
                }
            }
        }
示例#2
0
        public async Task <ProgressResult <T> > Do <T>(Func <ProgressDialogController, T> function, string message, bool canCancel, bool throwErrors)
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var controller = await modelHelpers.ShowIndeterminateProgressAsync("Please Wait", message, canCancel);

            var controllerCancel = Task.Run(() =>
            {
                while (true)
                {
                    if (!controller.IsOpen)
                    {
                        break;
                    }

                    if (controller.IsCanceled)
                    {
                        break;
                    }

                    Thread.Sleep(100);
                }
            }, cancellationTokenSource.Token);

            try
            {
                var functionTask = Task.Run(() => function.Invoke(controller), cancellationTokenSource.Token);

                ProgressResult <T> result;
                if (await Task.WhenAny(functionTask, controllerCancel) == controllerCancel)
                {
                    cancellationTokenSource.Cancel();
                    result = ProgressResult.GetCancelled <T>();
                }
                else
                {
                    if (functionTask.Status == TaskStatus.RanToCompletion)
                    {
                        result = ProgressResult.GetSuccess(functionTask.Result);
                    }
                    else
                    {
                        result = ProgressResult.GetErrored <T>();

                        if (throwErrors && functionTask.Exception != null)
                        {
                            throw functionTask.Exception.InnerException ?? functionTask.Exception;
                        }
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                if (throwErrors)
                {
                    throw;
                }

                ExceptionlessClient.Default.CreateEvent().SetException(ex).AddTags("Maybe-Hidden").Submit();
                return(ProgressResult.GetErrored <T>());
            }
            finally
            {
                cancellationTokenSource.Cancel();
                if (controller.IsOpen)
                {
                    await controller.CloseAsync();
                }
            }
        }