private void ThreadTimeSynch_Synch() { _isTimeSynchThreadRunning=true; while(_isTimeSynchThreadRunning) { if(!_isTimeSynchThreadRunning) { break; } NTPv4 ntp=new NTPv4(); double nistOffset=double.MaxValue; try { nistOffset=ntp.getTime(PrefC.GetString(PrefName.NistTimeServerUrl)); } catch { } //Invalid NIST Server URL if(nistOffset!=double.MaxValue) { //Did not timeout, or have invalid NIST server URL try { WindowsTime.SetTime(DateTime.Now.AddMilliseconds(nistOffset)); //Sets local machine time } catch { } //Error setting local machine time } _timeSynchSleep.WaitOne(TimeSpan.FromMinutes(240)); } }
///<summary>Get the offset from the nist server and DateTime.Now(). Returns double.MinValue if invalid NIST Server URL. Returns double.MaxValue if request timed out.</summary> private double GetNistOffset() { //NistTime NTPv4 ntp=new NTPv4(); double nistOffset; try { nistOffset=ntp.getTime(textNistUrl.Text); } catch { MsgBox.Show(this,"Invalid NIST Server URL"); return double.MinValue; } timer1.Enabled=true; return nistOffset; }