static void Main() { GiveInstructionsToTheUser(); OutputTheSummaryHeaders(); // Create and Start the background worker DoBackgroundwork bgw = new DoBackgroundwork(); bgw.StartWorker(); // Start the computation on the main thread. Each time through the loop, // check to see whether the user has cancelled the background thread. // After the calculation, add a short sleep, just to slow the program // down enough so the main thread doesn't run faster than the background. long mainTotal = 0; for (int i = 0; i < 5; i++) { if (Program.CheckForCancelInput()) { bgw.Cancel(); } mainTotal += DoBackgroundwork.CalculateTheSequence(100000000); Thread.Sleep(200); Console.WriteLine(" {0}%", (i + 1) * 20); } SummarizeResults(bgw, mainTotal); Console.ReadLine(); }
private static void SummarizeResults(DoBackgroundwork bgw, long mainTotal) { if (bgw.CompletedNormally) { Console.WriteLine("\nBackground completed Normally"); Console.WriteLine("Background total = {0}", bgw.BackgroundTotal); } else { Console.WriteLine("\nBackground Cancelled"); } Console.WriteLine("Main total = {0}", mainTotal); }