#if !NETSTANDARD1_3
namespace Unosquare.Swan.Components
{
using System;
using System.Threading;
using Abstractions;
///
/// 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
{
private readonly Timer _innerTimer;
private readonly IWaitEvent _delayLock = WaitEventFactory.Create(true);
///
/// Initializes a new instance of the class.
///
protected TimerControl()
{
_innerTimer = new Timer(
x =>
{
try
{
_delayLock.Complete();
_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)
_delayLock.Wait();
}
///
/// Waits the specified wait time.
///
/// The wait time.
/// The cancellation token.
public void Wait(TimeSpan waitTime, CancellationToken ct = default) =>
WaitUntil(DateTime.UtcNow.Add(waitTime), ct);
}
}
#endif