#if !NETSTANDARD1_3 using System; using System.Threading; using Unosquare.Swan.Abstractions; namespace Unosquare.Swan.Components { /// /// Use this singleton to wait for a specific TimeSpan or time. /// /// Internally this class will use a Timer and a ManualResetEvent to block until /// the time condition is satisfied. /// /// public class TimerControl : SingletonBase { [System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0052:Ungelesene private Member entfernen", Justification = "")] private readonly Timer _innerTimer; private readonly IWaitEvent _delayLock = WaitEventFactory.Create(true); /// /// Initializes a new instance of the class. /// protected TimerControl() => this._innerTimer = new Timer( x => { try { this._delayLock.Complete(); this._delayLock.Begin(); } catch { // ignore } }, null, 0, 15); /// /// Waits until the time is elapsed. /// /// The until date. /// The cancellation token. public void WaitUntil(DateTime untilDate, CancellationToken ct = default) { while(!ct.IsCancellationRequested && DateTime.UtcNow < untilDate) { this._delayLock.Wait(); } } /// /// Waits the specified wait time. /// /// The wait time. /// The cancellation token. public void Wait(TimeSpan waitTime, CancellationToken ct = default) => this.WaitUntil(DateTime.UtcNow.Add(waitTime), ct); } } #endif