C# Winform小程序:局域网设置NTP服务器、实现时间同步

  • 设置NTP服务器:

NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。

局域网不能连接Internet,可以设置一台计算机为NTP服务器。

  • 依次点击:开始---运行---regedit,进入注册表;
  • 依次展开:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer,在NtpServer项右侧键值Enablied,默认值0改为1,1为启动NTP服务器;

 

  • 依次展开:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config,在Config项右侧键值AnnounceFlags,默认值10改为5,5代表自身为可靠时间源;

  • 修改完成以上关闭注册表;
  • 命令行输入:net stop w32Time,回车,停止NTP服务;
  • 命令行输入:net start w32Time,回车,启动NTP服务;
  • 当前计算机设置NTP服务器完成。
  • 局域网内其他电脑命令行输入:net time \\10.30.100.119 /set /yes,实现时间同步,其中10.30.100.119代表NTP服务器地址。
  • C# Winform实现局域网时间同步demo:

全局变量:

//Timer
System.Timers.Timer timer = new System.Timers.Timer();
//IP
private string ipAddress;
//时间间隔
private int timeInterval;
//创建bat文件路径,如果存在,直接覆盖
private string filePath = System.Environment.CurrentDirectory + @"\timesync.bat";

页面Load方法:

private void FrmMain_Load(object sender,EventArgs e)
{
      //运行标志隐藏
      picRun.Visible = false;      
}

Run/Abort按钮点击事件:

private void btnRunAbort_Click(object sender,EventArgs e)
{
      //IP
      ipAddress = txtIP.Text.Trim();
      //Time Interval
      timeInterval = Convert.ToInt32(txtTimeInterval.Text);
      
      if (btnRunAbort.Text.Equals("开始同步"))
      {
            this.RunTimer();
      }
      else if (btnRunAbort.Text.Equals("停止同步"))
      {
            //定时器停止
            timer.Stop();
            timer = new System.Timers.Timer();

            MessageBox.Show("停止同步成功","提示");

            //运行标志隐藏
            picRun.Visible = false;
            btnRunAbort.Text = "开始同步";
      }
}

开始同步执行方法:

public void RunTimer()
{
      //创建bat文件
      FileStream fs = new FileStream(filePath, FileMode.Create,FileAccess.Write);
      StreamWriter sw = new StreamWriter(fs);
      sw.WriteLine(string.Format(@"net time \\{0} /set /yes",ipAddress));
      sw.Close();
      fs.Close();

      try
      {
            //执行时间超过10秒,强制结束
            if (!CallWithTimeOut(CommonRun,10000))
            {
                  MessageBox.Show("服务器异常!","提示");
                  return;
            }
            //服务器地址异常
            if (StringRun().EndsWith(@"/yes"))
            {
                  MessageBox.Show("服务器异常!","提示");
                  return;
            }

            MessageBox.Show("开始同步成功!","提示");
            //BeginInvoke
            SetPBState(true , "停止同步");
      }
      catch (Exception ex)
      {
            MessageBox.Show("服务器异常!","提示");
            return;
      }
      
      //timer定时器执行时间同步
      timer.Elapsed += new System.ElapsedEventHandle(TimerRun);
      timer.Enabled = true;
      timer.Interval = Convert.ToInt32(txtTimeInterval.Text) * 1000;
      timer.Start();
}

 时间同步,返回执行bat输出:

private string StringRun()
{
      ProcessStartInfo pro = new ProcessStartInfo("cmd.exe");
      pro.UseShellExecute = false;
      pro.RedirectStandardOutput = true;
      pro.RedirectStandardError = true;
      pro.CreateNoWindow = true;
      pro.FileName = filePath;
      pro.WorkingDirectory = System.Environment.CurrentDirectory;
      Process proc = new Process.Start(pro);

      StreamReader sOutput = proc.StandardOutput;
      proc.Close();
      
      string sJudge = sOutput.ReadToEnd().Trim();
      sOutput.Close();

      return sJudge;
}

时间同步:

private void CommonRun()
{
      ProcessStartInfo pro = new ProcessStartInfo("cmd.exe");
      pro.UseShellExecute = false;
      pro.RedirectStandardOutput = true;
      pro.RedirectStandardError = true;
      pro.CreateNoWindow = true;
      pro.FileName = filePath;
      pro.WorkingDirectory = System.Environment.CurrentDirectory;
      Process proc = new Process.Start(pro);

      StreamReader sOutput = proc.StandardOutput;
      proc.Close();
      
      string sJudge = sOutput.ReadToEnd().Trim();
      sOutput.Close();
}

timer定时器时间同步:

private void TimerRun(object source , System.Timers.ElapsedEventArgs e)
{
      ProcessStartInfo pro = new ProcessStartInfo("cmd.exe");
      pro.UseShellExecute = false;
      pro.RedirectStandardOutput = true;
      pro.RedirectStandardError = true;
      pro.CreateNoWindow = true;
      pro.FileName = filePath;
      pro.WorkingDirectory = System.Environment.CurrentDirectory;
      Process proc = new Process.Start(pro);

      StreamReader sOutput = proc.StandardOutput;
      proc.Close();
      
      string sJudge = sOutput.ReadToEnd().Trim();
      sOutput.Close();
}

deletegate InvokeRequired:

delegate void DelegatePBUpdate(bool b , string btnText);

public void SetPBState(bool b , string btnText)
{
      if (this.InvokeRequired)
      {
            this.BeginInvoke(new DelegatePBUpdate(SetPBState), new object[] {b , btnText});
      }
      else
      {
            picRun.Visible = true;
            btnRunAbort.Text = "停止同步";
      }
}

方法执行时间超过设定值,强制结束:

static bool CallWithTimeout(Action action, int timeoutMilliseconds)
{
      Thread threadToKill = null;
      Action wrappedAction = () =>
      {
            threadToKill = Thread.CurrentThread;
            action();
      }

      IAsyncResult result = wrappedAction.BeginInvoke(null, null);
      if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds))
      [
            wrappedAction.EndInvoke(result);
            return true;
      ]
      else
      {
            threadToKill.Abort();
            return false;
      }
}
  • 运行结果:

 

 

 

posted on 2018-01-30 16:59  奔跑的简单  阅读(3480)  评论(0编辑  收藏  举报

导航