C# Winform小程序:局域网设置NTP服务器、实现时间同步
生活随笔
收集整理的這篇文章主要介紹了
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) {//IPipAddress = txtIP.Text.Trim();//Time IntervaltimeInterval = 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("開始同步成功!","提示");//BeginInvokeSetPBState(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;} }- 運行結果:
?
?
?
轉載于:https://www.cnblogs.com/benpaodejiandan/p/8385448.html
總結
以上是生活随笔為你收集整理的C# Winform小程序:局域网设置NTP服务器、实现时间同步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AWS CSAA -- 04 AWS O
- 下一篇: 一个插件让你在 Redux 中写 pro