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); }
static void Main(string[] args) { WinsSync winssync = new WinsSync(); //Setup our credentials. winssync.SetupGoogleCreds(); try { winssync.LoadLoginCreds(); } catch (CryptographicException) { // Saved credentials are somehow corrupt, let's just delete them and get them from the user. winssync.DeleteLoginCreds(); winssync.LoadLoginCreds(); } // Time to log in and get our schedule. // Retry loop in case javascript execution breaks. It happens often enough, and we need to log out, and back in to recover. int retries = 5; while (retries > 0) { try { winssync.ScheduleGET(); break; } catch (ScheduleGETException ex) { Console.WriteLine(ex.ToString()); if (--retries == 0) { Console.WriteLine("We tried getting your schedule five times and it didn't work."); Console.WriteLine("Something must be up with your connection to the server."); Console.WriteLine("Please check your connection and try again. Press any key to quit."); if (winssync.Automate != "Automate") { Console.ReadKey(); } Environment.Exit(0); } } } // Parse the HTML for our schedule info. winssync.FindTable(); winssync.ParseTable(); winssync.ParseRows(); // Let's let the user look at their schedule while we're busy uploading. winssync.DisplayResults(); // Now we need to upload our schedule to Google. // Sometimes the upload request isn't processed correctly, just retrying fixes it almost every time. Console.WriteLine("Uploading your schedule now..."); retries = 5; while (retries > 0) { try { winssync.UploadResults().Wait(); break; } catch (Exception ex) { if (--retries == 0) { Console.WriteLine("We tried uploading your schedule multiple times and it didn't work."); Console.WriteLine("Here's a nasty error message to explain why:"); Console.Write(ex.ToString()); } } } Console.WriteLine("Upload Complete, Press Enter to exit."); //After a successful run with input, allow the user to enter "Automate" to remove all needed user input from future runs. //Don't prompt the user to use this mode, since it's a hassle to stop automated runs. if (winssync.Automate != "Automate") { if ((Console.ReadLine().Equals("Automate") && (winssync.Savedlogin == true))) { winssync.SaveLoginCreds("Automate"); winssync.AutomateRun(); Console.WriteLine("Alright, all future runs will require no input."); Console.WriteLine("To reset automation, manually delete the files at: "); Console.WriteLine(string.Empty); Console.WriteLine(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), ".credentials")); Console.WriteLine(string.Empty); Console.WriteLine("Press Any key to exit."); Console.ReadKey(); } } }