///<summary>Updates local time and Refreshes all time variables. Saves NIST server URL as preference.</summary> private void SynchTimes() { this.Cursor = Cursors.WaitCursor; //Get NistTime Offset double nistOffset = GetNistOffset(); if (nistOffset == double.MaxValue) //Timed out { MsgBox.Show(this, "No response received from NIST time server. Click synch time after four seconds."); this.Cursor = Cursors.Default; return; } if (nistOffset == double.MinValue) //Invalid Nist Server Address { this.Cursor = Cursors.Default; return; } double serverOffset = (MiscData.GetNowDateTimeWithMilli() - DateTime.Now).TotalMilliseconds; //Get current times from offsets _timeLocal = DateTime.Now; _timeServer = _timeLocal.AddMilliseconds(serverOffset); _timeNist = _timeLocal.AddMilliseconds(nistOffset); try { WindowsTime.SetTime(_timeNist); //Sets local machine time } catch { MsgBox.Show(this, "Error setting local machine time."); } _timeLocal = DateTime.Now; //Update time since it has now been set this.Cursor = Cursors.Default; refreshDisplays(); }
///<summary>Called from FormOpenDental.Load. Updates local time and checks to see if server time is in synch, with a fast db call (only acurate to seconds, not miliseconds).</summary> public bool TimesInSynchFast() { this.Cursor = Cursors.WaitCursor; textNistUrl.Text = PrefC.GetString(PrefName.NistTimeServerUrl); double nistOffset = GetNistOffset(); if (nistOffset == double.MaxValue) //Timed out { MsgBox.Show(this, "No response received from NIST time server. Click synch time after four seconds."); this.Cursor = Cursors.Default; return(false); } if (nistOffset == double.MinValue) //Invalid Nist Server Address { this.Cursor = Cursors.Default; return(false); } //Get current times from offsets _timeLocal = DateTime.Now; Stopwatch stopwatch = new Stopwatch(); //Used to keep NIST time in time even when system fails to set local machine time (Can't pull it later) stopwatch.Start(); _timeNist = _timeLocal.AddMilliseconds(nistOffset); try { WindowsTime.SetTime(_timeNist); //Sets local machine time } catch { MsgBox.Show(this, "Error setting local machine time."); } _timeLocal = DateTime.Now; //Update time since it has now been set double serverOffset = (MiscData.GetNowDateTime() - DateTime.Now).TotalSeconds; //Cannot get milliseconds from Now() in Mysql Pre-5.6.4, Only gets whole seconds. _timeServer = _timeLocal.AddSeconds(serverOffset); if (ServerInSynchFast() && LocalInSynch()) //All times in synch { return(true); } //Some times out of synch, so form will open, but we don't want to make another call to NIST server. serverOffset = (MiscData.GetNowDateTimeWithMilli() - DateTime.Now).TotalSeconds; //_timeServer needs to be more accurate before displaying _timeLocal = DateTime.Now; stopwatch.Stop(); _timeServer = _timeLocal.AddSeconds(serverOffset); _timeNist = _timeNist.AddMilliseconds(stopwatch.ElapsedMilliseconds); this.Cursor = Cursors.Default; return(false); }