Source

KeeOtp / Troubleshooting.cs

Diff from to

Troubleshooting.cs

 using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Data;
 using System.Diagnostics;
-using System.Drawing;
-using System.Linq;
-using System.Net;
-using System.Text;
+using System.Threading;
 using System.Windows.Forms;
+using OtpSharp;
 
 namespace KeeOtp
 {
 
         private void buttonPingGoogle_Click(object sender, EventArgs e)
         {
-            try
+            // this is kind of an odd flow.
+
+            TimeCorrection timeCorrection = null;
+
+            // set up the asyn operation complete with continuation, catch and finally delegates
+            // the async operation will use the synchronization context to post the continuation,
+            // catch, and finally delegates to run on the UI thread.  Since we are creating this object
+            // on the UI thread it will use the UI synchronization context.
+            AsyncOperation getTimeCorrectionOperation = new AsyncOperation(() => // on background threadpool thread
             {
-                using (var wc = new WebClient())
-                {
-                    wc.DownloadData("https://www.google.com");
-                    var dateHeader = wc.ResponseHeaders.Get("Date");
-                    var date = DateTime.Parse(dateHeader);
+                timeCorrection = Ntp.GetTimeCorrectionFromGoogle();
+            },
+            () => // continuation on UI thread
+            {
+                var offset = timeCorrection.CorrectionFactor;
 
-                    var offset = date.ToUniversalTime() - DateTime.UtcNow;
+                var totalSeconds = Math.Abs(offset.TotalSeconds);
+                if (totalSeconds == 0)
+                    MessageBox.Show("Your time is perfect according to Google's servers");
+                else if (totalSeconds <= 5)
+                    MessageBox.Show("Your time is off by five seconds or less from Google's servers.  You should be just fine.");
+                else if (totalSeconds <= 30)
+                    MessageBox.Show(string.Format("Your time is off by {0} seconds from Google's servers.  You are probably OK but correcting couldn't hurt.", totalSeconds));
+                else
+                    MessageBox.Show(string.Format("Your time is off by {0} seconds from Google's servers.  Try correcting the difference and try again.", totalSeconds));
+            },
+            ex => MessageBox.Show(ex.Message, "Error"), // catch on UI thread
+            () => // finally on UI thread
+            {
+                this.buttonPingGoogle.Visible = true;
+                this.progressBarGettingTimeCorrection.Visible = false;
+            });
 
-                    if (offset.TotalSeconds == 0)
-                        MessageBox.Show("Your time is perfect according to Google's servers");
-                    else if (offset.TotalSeconds <= 5)
-                        MessageBox.Show("Your time is off by five seconds or less from Google's servers.  You should be just fine.");
-                    else
-                        MessageBox.Show(string.Format("Your time is off by {0} seconds from Google's servers.  Try correcting the difference and try again.", offset.TotalSeconds));
-                }
-            }
-            catch (Exception ex)
-            {
-                MessageBox.Show(ex.Message, "Error");
-            }
+
+            this.buttonPingGoogle.Visible = false;
+            this.progressBarGettingTimeCorrection.Visible = true;
+
+            getTimeCorrectionOperation.Run();
         }
 
         private void buttonTroubleshootingWebsite_Click(object sender, EventArgs e)