#region CPL License /* Nuclex Framework Copyright (C) 2002-2009 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion #if !XBOX360 using System; using System.Collections.Generic; using System.Threading; using Microsoft.Win32; namespace Nuclex.Support.Scheduling { /// /// Time source that makes use of additional features only available on Windows /// public class WindowsTimeSource : GenericTimeSource, IDisposable { /// Number of ticks (100 ns intervals) in a millisecond private const long TicksPerMillisecond = 10000; /// Initializes a new Windows time source public WindowsTimeSource() { this.onDateTimeAdjustedDelegate = new EventHandler(OnDateTimeAdjusted); SystemEvents.TimeChanged += this.onDateTimeAdjustedDelegate; } /// Immediately releases all resources owned by the instance public void Dispose() { if(this.onDateTimeAdjustedDelegate != null) { SystemEvents.TimeChanged -= this.onDateTimeAdjustedDelegate; this.onDateTimeAdjustedDelegate = null; } } /// Waits for an AutoResetEvent to become signalled /// WaitHandle the method will wait for /// Number of ticks to wait /// /// True if the WaitHandle was signalled, false if the timeout was reached /// public override bool WaitOne(AutoResetEvent waitHandle, long ticks) { return waitHandle.WaitOne((int)(ticks / TicksPerMillisecond)); } /// /// Whether the Windows time source can be used on the current platform /// public static bool Available { get { return Environment.OSVersion.Platform == PlatformID.Win32NT; } } /// Delegate for the timeChanged() callback method private EventHandler onDateTimeAdjustedDelegate; } } // namespace Nuclex.Support.Scheduling #endif // !XBOX360