/// <summary> /// Executes a <see cref="Func{Task}"/> which returns a <see cref="Task{TOutput}"/> /// </summary> /// <param name="processor"><see cref="Processor"/></param> /// <param name="func">The function to execute</param> /// <returns><see cref="IResponse"/></returns> public static IResponse <TOutput> Process <TOutput>(this Processor processor, Func <Task <TOutput> > func) { if (processor == null) { var processNullMessage = $"The provided {nameof(processor)} is null."; SweetAlerts.Alert(string.Empty, processNullMessage, AlertButtons.Ok, AlertType.Error); return(ResponseFactory <TOutput> .Error(processNullMessage, ErrorResponseStatus.BadRequest)); } try { using (var form = new FuncOutputExecutorTask <TOutput> { ParentControl = processor.ParentControl, FormTitle = processor.OperationTitle, CanRetry = processor.CanRetry, ShowSuccessMessage = processor.ReportSuccess, IgnoreResponseMessage = processor.IgnoreResponseMessage, SuccessMessage = processor.SuccessMessage, Func = func, CancellationTokenSource = processor.CancellationTokenSource, FormImage = Properties.Resources.rolling, ProgressObject = processor.ProgressObject }) { form.ShowDialog(); return(form.Response as IResponse <TOutput>); } } catch (Exception ex) { return(ResponseFactory <TOutput> .Exception(ex)); } }
/// <summary> /// Executes a function /// </summary> /// <param name="processor">The <see cref="Processor"/></param> /// <param name="func">The function to execute</param> /// <returns><see cref="IResponse"/></returns> public static IResponse Process(this Processor processor, Func <IResponse> func) { if (processor == null) { var processNullMessage = $"The provided {nameof(processor)} is null."; SweetAlerts.Alert(HelperMethods.GetCurrentlyActiveForm(), string.Empty, processNullMessage, AlertButtons.Ok, AlertType.Error); return(ResponseFactory.Error(processNullMessage, ErrorResponseStatus.BadRequest)); } try { using (var form = new FuncOutputResponseExecutor { ParentControl = HelperMethods.GetCurrentlyActiveForm(processor.ParentControl), FormTitle = processor.OperationTitle, CanRetry = processor.CanRetry, ShowSuccessMessage = processor.ReportSuccess, IgnoreResponseMessage = processor.IgnoreResponseMessage, SuccessMessage = processor.SuccessMessage, Func = func, CancellationTokenSource = processor.CancellationTokenSource, FormImage = Properties.Resources.rolling, ProgressObject = processor.ProgressObject }) { form.ShowDialog(HelperMethods.GetCurrentlyActiveForm(processor.ParentControl)); return(form.Response); } } catch (Exception ex) { return(ResponseFactory.Exception(ex)); } }
private void AlertFormControl_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.F12 && !string.IsNullOrWhiteSpace(ExceptionDetail)) { SweetAlerts.ShowInfo(this, "Exception Detail", ExceptionDetail); } }
private async Task ExecuteAsync() { while (true) { Response = await ExecuteRequestAsync(); if (!Response.Success) { if (Retryable) { var retrySelection = SweetAlerts.Alert(OperationTitle, Response.Messages.ToList(), AlertButtons.RetryCancel, AlertType.Error); if (retrySelection != DialogResult.Retry) { return; } continue; } } SweetAlerts.AlertResponse(OperationTitle, Response, ShowSuccessMessage, IgnoreResponseMessage, SuccessMessage); break; } }
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; } }