39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Threading;
|
|
|
|
namespace HashCalculator.GUI
|
|
{
|
|
internal class DisposableDispatcherTimer : IDisposable
|
|
{
|
|
public DispatcherTimer Timer { get; }
|
|
public DateTimeOffset CreationTime { get; } = DateTimeOffset.UtcNow;
|
|
public TimeSpan TimeSinceCreation => DateTimeOffset.UtcNow - CreationTime;
|
|
|
|
public DisposableDispatcherTimer(Action<DisposableDispatcherTimer> action, Dispatcher dispatcher, TimeSpan interval) :
|
|
this(action, dispatcher, interval, TimeSpan.Zero)
|
|
{
|
|
}
|
|
|
|
public DisposableDispatcherTimer(Action<DisposableDispatcherTimer> action, Dispatcher dispatcher, TimeSpan interval, TimeSpan wait)
|
|
{
|
|
Timer = new DispatcherTimer(interval, DispatcherPriority.Normal, (s, e) =>
|
|
{
|
|
if (Timer.IsEnabled && (TimeSinceCreation > wait))
|
|
{
|
|
action(this);
|
|
}
|
|
}, dispatcher);
|
|
Timer.Start();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Timer.Stop();
|
|
}
|
|
}
|
|
}
|