Comment and split HiResTimer

This commit is contained in:
Matt Nadareski
2021-06-06 21:38:15 -07:00
parent 13ba103a64
commit f27f455d85
2 changed files with 26 additions and 19 deletions

View File

@@ -4,12 +4,15 @@ using System.Threading;
namespace RedBookPlayer namespace RedBookPlayer
{ {
/// <summary>
/// Recurring timer wrapper with a high degree of accuracy
/// </summary>
public class HiResTimer public class HiResTimer
{ {
static readonly float tickFrequency = 1000f / Stopwatch.Frequency; static readonly float tickFrequency = 1000f / Stopwatch.Frequency;
volatile float interval; volatile float _interval;
volatile bool isRunning; volatile bool _isRunning;
public HiResTimer() : this(1f) {} public HiResTimer() : this(1f) {}
@@ -19,24 +22,25 @@ namespace RedBookPlayer
float.IsNaN(interval)) float.IsNaN(interval))
throw new ArgumentOutOfRangeException(nameof(interval)); throw new ArgumentOutOfRangeException(nameof(interval));
this.interval = interval; _interval = interval;
} }
public float Interval public float Interval
{ {
get => interval; get => _interval;
set set
{ {
if(value < 0f || if(value < 0f ||
float.IsNaN(value)) float.IsNaN(value))
throw new ArgumentOutOfRangeException(nameof(value)); throw new ArgumentOutOfRangeException(nameof(value));
interval = value; _interval = value;
} }
} }
public bool Enabled public bool Enabled
{ {
get => _isRunning;
set set
{ {
if(value) if(value)
@@ -44,23 +48,22 @@ namespace RedBookPlayer
else else
Stop(); Stop();
} }
get => isRunning;
} }
public event EventHandler<HiResTimerElapsedEventArgs> Elapsed; public event EventHandler<HiResTimerElapsedEventArgs> Elapsed;
public void Start() public void Start()
{ {
if(isRunning) if(_isRunning)
return; return;
isRunning = true; _isRunning = true;
var thread = new Thread(ExecuteTimer); var thread = new Thread(ExecuteTimer);
thread.Priority = ThreadPriority.Highest; thread.Priority = ThreadPriority.Highest;
thread.Start(); thread.Start();
} }
public void Stop() => isRunning = false; public void Stop() => _isRunning = false;
void ExecuteTimer() void ExecuteTimer()
{ {
@@ -69,9 +72,9 @@ namespace RedBookPlayer
var stopwatch = new Stopwatch(); var stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
while(isRunning) while(_isRunning)
{ {
nextTrigger += interval; nextTrigger += _interval;
float elapsed; float elapsed;
while(true) while(true)
@@ -91,7 +94,7 @@ namespace RedBookPlayer
else else
Thread.Sleep(10); Thread.Sleep(10);
if(!isRunning) if(!_isRunning)
return; return;
} }
@@ -110,11 +113,4 @@ namespace RedBookPlayer
static float ElapsedHiRes(Stopwatch stopwatch) => stopwatch.ElapsedTicks * tickFrequency; static float ElapsedHiRes(Stopwatch stopwatch) => stopwatch.ElapsedTicks * tickFrequency;
} }
public class HiResTimerElapsedEventArgs : EventArgs
{
internal HiResTimerElapsedEventArgs(float delay) => Delay = delay;
public float Delay { get; }
}
} }

View File

@@ -0,0 +1,11 @@
using System;
namespace RedBookPlayer
{
public class HiResTimerElapsedEventArgs : EventArgs
{
internal HiResTimerElapsedEventArgs(float delay) => Delay = delay;
public float Delay { get; }
}
}