Created
August 18, 2010 22:34
-
-
Save cstrahan/536409 to your computer and use it in GitHub Desktop.
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
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| public class DateTimeElapsedEventArgs : EventArgs | |
| { | |
| public DateTime DateTime { get; private set; } | |
| public DateTimeElapsedEventArgs(DateTime dateTime) | |
| { | |
| DateTime = dateTime; | |
| } | |
| } |
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
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Management; | |
| using System.Timers; | |
| public class DateTimer : IDisposable | |
| { | |
| public int? Year | |
| { | |
| get { return _year; } | |
| set | |
| { | |
| _year = value; | |
| UpdateWatcherIfEnabled(); | |
| } | |
| } | |
| public int? Month | |
| { | |
| get { return _month; } | |
| set | |
| { | |
| _month = value; | |
| UpdateWatcherIfEnabled(); | |
| } | |
| } | |
| public int? Day | |
| { | |
| get { return _day; } | |
| set | |
| { | |
| _day = value; | |
| UpdateWatcherIfEnabled(); | |
| } | |
| } | |
| public int? Hour | |
| { | |
| get { return _hour; } | |
| set | |
| { | |
| _hour = value; | |
| UpdateWatcherIfEnabled(); | |
| } | |
| } | |
| public int? Minute | |
| { | |
| get { return _minute; } | |
| set | |
| { | |
| _minute = value; | |
| UpdateWatcherIfEnabled(); | |
| } | |
| } | |
| public int? Second | |
| { | |
| get { return _second; } | |
| set | |
| { | |
| _second = value; | |
| UpdateWatcherIfEnabled(); | |
| } | |
| } | |
| public bool Enabled | |
| { | |
| get | |
| { | |
| return _enabled; | |
| } | |
| set | |
| { | |
| _enabled = value; | |
| UpdateWatcherIfEnabled(); | |
| } | |
| } | |
| private int? _year; | |
| private int? _month; | |
| private int? _day; | |
| private int? _hour; | |
| private int? _minute; | |
| private int? _second; | |
| private bool _enabled; | |
| private ManagementEventWatcher _watcher; | |
| public event EventHandler<DateTimeElapsedEventArgs> Elapsed; | |
| public DateTimer() | |
| { | |
| } | |
| public DateTimer(int? year = null, int? month = null, int? day = null, | |
| int? hour = null, int? minute = null, int? second = null) | |
| { | |
| _year = year; | |
| _month = month; | |
| _day = day; | |
| _hour = hour; | |
| _minute = minute; | |
| _second = second; | |
| } | |
| private string BuildQuery(int? year, int? month, int? day, int? hour, int? minute, int? second) | |
| { | |
| List<string> parts = new List<string>(); | |
| if (year != null) parts.Add("TargetInstance.Year = " + year); | |
| if (month != null) parts.Add("TargetInstance.Month = " + month); | |
| if (day != null) parts.Add("TargetInstance.Day = " + day); | |
| if (hour != null) parts.Add("TargetInstance.Hour = " + hour); | |
| if (minute != null) parts.Add("TargetInstance.Minute = " + minute); | |
| if (second != null) parts.Add("TargetInstance.Second = " + second); | |
| string query = @" | |
| SELECT * | |
| FROM __InstanceModificationEvent | |
| WHERE TargetInstance ISA 'Win32_LocalTime' | |
| AND | |
| ( " + string.Join(" AND ", parts) + ")"; | |
| return query; | |
| } | |
| private void HandleEventArrived(object sender, EventArrivedEventArgs e) | |
| { | |
| var elapsed = Elapsed; | |
| if (elapsed != null) | |
| { | |
| var instance = (ManagementBaseObject)e.NewEvent["TargetInstance"]; | |
| var dateTime = new DateTime( | |
| (int)(uint)instance["Year"], | |
| (int)(uint)instance["Month"], | |
| (int)(uint)instance["Day"], | |
| (int)(uint)instance["Hour"], | |
| (int)(uint)instance["Minute"], | |
| (int)(uint)instance["Second"]); | |
| var args = new DateTimeElapsedEventArgs(dateTime); | |
| elapsed(this, args); | |
| } | |
| } | |
| private void UpdateWatcherIfEnabled() | |
| { | |
| if (_enabled) | |
| { | |
| if (_watcher != null) | |
| { | |
| _watcher.Stop(); | |
| _watcher.EventArrived -= HandleEventArrived; | |
| _watcher.Dispose(); | |
| _watcher = null; | |
| } | |
| if (Year != null || Month != null || Day != null || | |
| Hour != null || Minute != null || Second != null) | |
| { | |
| var query = BuildQuery(Year, Month, Day, Hour, Minute, Second); | |
| _watcher = | |
| new ManagementEventWatcher(new WqlEventQuery(query)); | |
| _watcher.EventArrived += HandleEventArrived; | |
| _watcher.Start(); | |
| } | |
| } | |
| } | |
| public void Start() | |
| { | |
| _enabled = true; | |
| UpdateWatcherIfEnabled(); | |
| } | |
| public void Stop() | |
| { | |
| _watcher.Stop(); | |
| _watcher.EventArrived -= HandleEventArrived; | |
| _watcher.Dispose(); | |
| _watcher = null; | |
| } | |
| public void Dispose() | |
| { | |
| Stop(); | |
| } | |
| } |
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
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Management; | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var now = DateTime.Now; | |
| Console.WriteLine("The time is now {0}", now); | |
| var aCoupleSecondsLater = now.AddSeconds(10); | |
| using (var timer = new DateTimer(day: aCoupleSecondsLater.Day, | |
| second: aCoupleSecondsLater.Second)) | |
| { | |
| timer.Elapsed += timer_Elapsed; | |
| timer.Enabled = true; | |
| Console.WriteLine("Press any key to exit . . ."); | |
| Console.ReadKey(true); | |
| } | |
| } | |
| static void timer_Elapsed(object sender, DateTimeElapsedEventArgs e) | |
| { | |
| var timer = (DateTimer)sender; | |
| timer.Second = e.DateTime.AddSeconds(2).Second; | |
| Console.WriteLine(e.DateTime); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment