• 设置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,))
{
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) * ;
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;
}
}
  • 运行结果:

C# Winform小程序:局域网设置NTP服务器、实现时间同步的更多相关文章

  1. visual studio 设计第一个WinForm小程序

    WinForm小程序之消息框 首先打开visual studio 软件,然后[文件]-[新建]-[项目]-[Visual C#]-[Windows],选择Windows窗体应用程序,根据自己的需要修改 ...

  2. 如何在 CentOS 中设置 NTP 服务器

    网络时间协议(NTP)用来同步网络上不同主机的系统时间.你管理的所有主机都可以和一个指定的被称为 NTP 服务器的时间服务器同步它们的时间.而另一方面,一个 NTP 服务器会将它的时间和任意公共 NT ...

  3. 微信小程序wxss设置样式

    微信小程序wxss设置样式 对于以前搞客户端开发的来说,有着客户端的逻辑,就是不知道怎么设置样式,把对应的控件显示出来 一.wxml 界面结构wxmL比较容易理解,主要是由八大类基础组件构成: 一.视 ...

  4. C# WinForm小程序(技术改变世界-cnblog)

    WinForm小程序(技术改变世界-cnblog)   需求: 1.点击按钮  更新 当前时间 2.输入 身份证,必须身份证 排序(类似银行卡那样的空格),自动生成空格排序 3.实现 必须按 第一个按 ...

  5. 微信小程序警告设置 enable-flex 属性以使 flexbox 布局生效的解决办法

    微信小程序警告设置 enable-flex 属性以使 flexbox 布局生效的解决办法 具体情况: scroll-view 滚动,设置 display:flex 不生效并警告设置 enable-fl ...

  6. CentOS 7.2部署NTP服务器实现时间同步

    CentOS 7.2部署NTP服务器实现时间同步 [日期:2017-12-18] 来源:Linux社区  作者:梁明远 [字体:大 中 小]   1. 前言 对于容器编排系统,前段时间主要研究kube ...

  7. 部署NTP服务器进行时间同步

    NTP服务端:linl_S    IP:10.0.0.15 NTP客户端:lin_C    IP:10.0.0.16 NTP服务概述 1.原理 NTP(Network TimeProtocol,网络时 ...

  8. 微信小程序如何设置服务器配置

    最近微信小程序在it界火了起来,公司也要求我们开始接触微信小程序,废话不多说直接从配置微信小程序开始 1,首先,登录 https://mp.weixin.qq.com,(这里默认你已经获取到微信小程序 ...

  9. 微信小程序-关于post 过来服务器没有获取到数据问题

    查看一下服务器接收的post 参数是以什么形式接收的 微信给出得demo 请求的方式是Json 参数传递的 所以如果服务器使用的from 表单形式接收数据需要更改微信小程序中的 header 将 he ...

随机推荐

  1. 用Hadoop,还是不用Hadoop?

    本文由 伯乐在线 - Lex Lian 翻译.英文出处:Anand Krishnaswamy.欢迎加入翻译小组. Hadoop通常被认定是能够帮助你解决所有问题的唯一方案. 当人们提到“大数据”或是“ ...

  2. winform跨线程问题(有参数和无参数)

    1.invoke是同步线程 using System; using System.Collections.Generic; using System.ComponentModel; using Sys ...

  3. (转)取消目录与SVN的关联

    第一种方法: 直接.逐级地删除目标目录中的隐藏属性的.svn目录 第二种方法: 如果用的是TortoiseSVN客户端,则先在另外一处建立一个新目录A,右键点住svn目录并拖动到A上松手,在弹出的菜单 ...

  4. python之控制流习题+代码

    # !/usr/bin/python3 # -*- coding: utf-8 -*- # @Time :2018/11/26 15:32 # @Author :yosef # @Email :wur ...

  5. PHP7 关于变量的基本判断

    刚学 PHP ,一些基础还不太牢固,边实践边记录. about NULL $class_name = null; 语句结束后,$class_name 是空,没有,什么都没有的“空”.用 is_null ...

  6. 洛谷 P2515 [HAOI2010]软件安装(缩点+树形dp)

    题面 luogu 题解 缩点+树形dp 依赖关系可以看作有向边 因为有环,先缩点 缩点后,有可能图不联通. 我们可以新建一个结点连接每个联通块. 然后就是树形dp了 Code #include< ...

  7. C++ STL之Set

    set是关联容器,类似于集合. 特点是里面的元素不会重复,而且元素时有序的. 1.声明定义: #include<set> using namespace std; set<int&g ...

  8. [转] electron实战开发详细流程

    [From] http://www.myk3.com/arc-8856.html 很久没有更新博客,人越来越懒了,唉 说好做的electron教程距离上次玩electron已经过去了好几个月了.. 这 ...

  9. [转] Emmet-前端开发神器

    [From] https://segmentfault.com/a/1190000007812543 Emmet是一款编辑器插件,支持多种编辑器支持.在前端开发中,Emmet 使用缩写语法快速编写 H ...

  10. pymongodb的使用和一个腾讯招聘爬取的案例

    一.在python3中操作mongodb 1.连接条件 安装好pymongo库 启动mongodb的服务端(如果是前台启动后就不关闭窗口,窗口关闭后服务端也会跟着关闭) 3.使用 import pym ...