private async void ButtonLoadOnClicked(object sender, EventArgs e) { ViewModel.ReportGenerationCancelationTokenSource = new CancellationTokenSource(); ViewModel.IsGenerating = true; _generationTask = Task.Run(async() => { try { var report = await ViewModel.ActionGenerateReportAsync(ViewModel.ReportGenerationCancelationTokenSource.Token); Application.Invoke((s, eventArgs) => { ViewModel.Report = report; }); } catch (OperationCanceledException) { Application.Invoke((s, eventArgs) => { ViewModel.ShowWarning("Формирование отчета было прервано"); }); } catch (Exception ex) { Application.Invoke((s, eventArgs) => throw ex); } finally { Application.Invoke((s, eventArgs) => { ViewModel.IsGenerating = false; }); } }, ViewModel.ReportGenerationCancelationTokenSource.Token); await _generationTask; }