Last active
April 22, 2016 11:29
-
-
Save hb9fxq/1fe836c4f151d9920eefd2a38252cfdc to your computer and use it in GitHub Desktop.
quick & dirty timediff using w32tm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*Not for production- Zombie processes!!! */ | |
| namespace NtpTimeDiff | |
| { | |
| using System; | |
| using System.ComponentModel; | |
| using System.Diagnostics; | |
| using System.Text.RegularExpressions; | |
| using System.Windows.Forms; | |
| public partial class Form1 : Form | |
| { | |
| delegate void LblTextSetCallbackDelegate(string text, string text2, string text3); | |
| public Form1() | |
| { | |
| InitializeComponent(); | |
| } | |
| private void button1_Click(object sender, EventArgs e) | |
| { | |
| var regEx = new Regex(@"([-+]?[0-9]*\.?[0-9]*)s"); | |
| var bw = new BackgroundWorker {WorkerReportsProgress = true}; | |
| bw.DoWork += delegate | |
| { | |
| var proc = new Process | |
| { | |
| StartInfo = new ProcessStartInfo | |
| { | |
| FileName = "w32tm", | |
| Arguments = "/stripchart /computer:" + textBox1.Text, | |
| UseShellExecute = false, | |
| RedirectStandardOutput = true, | |
| CreateNoWindow = true | |
| } | |
| }; | |
| proc.Start(); | |
| while (!proc.StandardOutput.EndOfStream) | |
| { | |
| var ln = proc.StandardOutput.ReadLine(); | |
| if (null == ln || (!ln.Contains("d:") || !ln.Contains("o:")) || !regEx.IsMatch(ln)) continue; | |
| var match = regEx.Matches(ln); | |
| if (this.vlblDiff.InvokeRequired) | |
| { | |
| var d = new LblTextSetCallbackDelegate(SetText); | |
| this.Invoke(d, match[0].Value, match[1].Value, ln); | |
| } | |
| else | |
| { | |
| this.vlblDiff.Text = regEx.Match(ln).Groups[0].Value; | |
| this.vlblOffset.Text = regEx.Match(ln).Groups[1].Value; | |
| this.vlblRaw.Text = ln; | |
| } | |
| } | |
| }; | |
| bw.RunWorkerAsync(); | |
| } | |
| private void SetText(string tdiff, string tOff, string tRaw) | |
| { | |
| if (this.vlblDiff.InvokeRequired) | |
| { | |
| var d = new LblTextSetCallbackDelegate(SetText); | |
| this.Invoke(d, new object[] { tdiff, tOff, tRaw }); | |
| } | |
| else | |
| { | |
| this.vlblDiff.Text = tdiff; | |
| this.vlblOffset.Text = tOff; | |
| this.vlblRaw.Text = tRaw; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment