private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { winsSync.SetupGoogleCreds(); backgroundWorker.ReportProgress(20); int retries = 5; while (retries > 0) { try { winsSync.ScheduleGET(); break; } catch (ScheduleGETException ex) { // Check to see if a cancellation request is pending. If it is, clean up then return. //todo: Make this check a function since it's repeated. if (backgroundWorker.CancellationPending) { backgroundWorker.ReportProgress(0); winsSync.ClearSchedules(); return; } if (--retries == 0) { // Failed 5 times, reset progress state and start over. backgroundWorker.ReportProgress(0); string message = ex.ToString(); MessageBox.Show(message); winsSync.ClearSchedules(); return; } } } if (backgroundWorker.CancellationPending) { backgroundWorker.ReportProgress(0); winsSync.ClearSchedules(); return; } backgroundWorker.ReportProgress(60); winsSync.FindTable(); winsSync.ParseTable(); winsSync.ParseRows(); backgroundWorker.ReportProgress(80); retries = 5; while (retries > 0) { try { winsSync.UploadResults().Wait(); break; } catch (Exception ex) { if (backgroundWorker.CancellationPending) { backgroundWorker.ReportProgress(0); winsSync.ClearSchedules(); return; } if (--retries == 0) { // Failed 5 times, reset progress state and start over. string message = ex.ToString(); MessageBox.Show(message); backgroundWorker.ReportProgress(0); winsSync.ClearSchedules(); return; } } } winsSync.ClearSchedules(); backgroundWorker.ReportProgress(100); }