예제 #1
0
        private async Task ExecuteAsync()
        {
            while (true)
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();
                Visible  = true;
                Response = await ExecuteRequestAsync();

                stopWatch.Stop();
                var stopWatchSeconds = stopWatch.Elapsed.TotalSeconds;
                if (stopWatchSeconds < 1)
                {
                    //If process was very quick, add a second delay to avoid sudden flickering
                    await Task.Delay(TimeSpan.FromSeconds(1));
                }

                Visible = false;

                if (!Response.Success)
                {
                    //Break if operation was cancelled
                    if (Response.Cancelled)
                    {
                        break;
                    }

                    if (CanRetry)
                    {
                        var buttons        = Response.Cancelled ? AlertButtons.Ok : AlertButtons.RetryCancel;
                        var retrySelection = AlertDisplayHandler.Alert(this, FormTitle, Response.Messages.ToList(),
                                                                       Response.Title, SweetAlerts.ExceptionDetail(Response),
                                                                       AlertType.Error, buttons);

                        if (retrySelection != DialogResult.Retry)
                        {
                            return;
                        }
                        if (MessagesRichTextBox != null)
                        {
                            MessagesRichTextBox.Text = string.Empty;
                        }

                        continue;
                    }

                    AlertDisplayHandler.Alert(this, FormTitle, Response.Messages.ToList(),
                                              Response.Title, SweetAlerts.ExceptionDetail(Response),
                                              AlertType.Error, AlertButtons.Ok);
                    break;
                }

                //Break if operation was cancelled
                if (Response.Cancelled)
                {
                    break;
                }

                var rtf = MessagesRichTextBox != null &&
                          !string.IsNullOrWhiteSpace(MessagesRichTextBox.Text)
                    ? MessagesRichTextBox.Rtf
                    : string.Empty;

                var successMessages = Response.Messages.ToList();
                if (!successMessages.Any())
                {
                    successMessages.Add("Processed successfully");
                }

                if (ShowSuccessMessage)
                {
                    if (IgnoreResponseMessage)
                    {
                        successMessages = new List <string>
                        {
                            !string.IsNullOrWhiteSpace(SuccessMessage) ? SuccessMessage : "Processed successfully"
                        };
                    }

                    AlertDisplayHandler.AlertRtf(this, FormTitle, successMessages, Response.Title,
                                                 string.Empty, rtf, AlertType.Success, AlertButtons.Ok);
                }
                break;
            }
        }