private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { if (!(sender is BackgroundWorker worker)) { return; } var n = (int)e.Argument; try { e.Result = _webService.Fibonacci(n); } catch (System.ServiceModel.CommunicationException exception) { MessageBox.Show("Connection Error", "Oups", MessageBoxButtons.OK); throw new Exception("Connection Error", exception); } catch (Exception exception) { MessageBox.Show("An error has ocurred", "Oups", MessageBoxButtons.OK); throw new Exception("Connection Error", exception); } for (int i = 1; i <= n; i++) { if (worker.CancellationPending) { e.Cancel = true; break; } System.Threading.Thread.Sleep(250); worker.ReportProgress(i * 100 / n); } }
public decimal Fibonacci(int n) { return(_webService.Fibonacci(n)); }