Exemple #1
0
        private void TimeSync()
        {
            double error = 0;

            try
            {
                Log.overlayLog($"时间同步:NTP请求发送");
                var offset = new NtpClient(Settings.Default.NtpServer).GetOffset(out error);
                Log.overlayLog($"时间同步:与北京时间相差{offset.Milliseconds}毫秒");
                //TODO:error handler
                if (CommonUtilities.SetSystemDateTime.SetLocalTimeByStr(
                        DateTime.Now.AddMilliseconds(offset.TotalMilliseconds * -0.5)))
                {
                    tlblTime.Text = "本地时钟已同步";
                }
                else
                {
                    tlblTime.Text = "设置系统时钟失败";
                }
            }
            catch (Exception e)
            {
                tlblTime.Text = "获取网络时钟失败: " + error.ToString() + ": " + e.ToString();
            }
        }
Exemple #2
0
        private void btnTimeSync_Click(object sender, EventArgs e)
        {
            double error  = 0;
            var    offset = new NtpClient("ntp3.aliyun.com").GetOffset(out error);

            if (CommonUtilities.SetSystemDateTime.SetLocalTimeByStr(DateTime.Now.AddMilliseconds(offset.TotalMilliseconds * -0.5)))
            {
                timeLabel.Text = $"已同步 误差{error}ms";
            }
            else
            {
                timeLabel.Text = $"设置时间出错";
            }
        }