Remove unneeded code

This commit is contained in:
Matt Nadareski
2021-06-29 14:24:13 -07:00
parent 0207448d76
commit 3018b374ab
2 changed files with 0 additions and 127 deletions

View File

@@ -1,116 +0,0 @@
using System;
using System.Diagnostics;
using System.Threading;
namespace RedBookPlayer
{
/// <summary>
/// Recurring timer wrapper with a high degree of accuracy
/// </summary>
public class HiResTimer
{
static readonly float tickFrequency = 1000f / Stopwatch.Frequency;
volatile float _interval;
volatile bool _isRunning;
public HiResTimer() : this(1f) {}
public HiResTimer(float interval)
{
if(interval < 0f ||
float.IsNaN(interval))
throw new ArgumentOutOfRangeException(nameof(interval));
_interval = interval;
}
public float Interval
{
get => _interval;
set
{
if(value < 0f ||
float.IsNaN(value))
throw new ArgumentOutOfRangeException(nameof(value));
_interval = value;
}
}
public bool Enabled
{
get => _isRunning;
set
{
if(value)
Start();
else
Stop();
}
}
public event EventHandler<HiResTimerElapsedEventArgs> Elapsed;
public void Start()
{
if(_isRunning)
return;
_isRunning = true;
var thread = new Thread(ExecuteTimer);
thread.Priority = ThreadPriority.Highest;
thread.Start();
}
public void Stop() => _isRunning = false;
void ExecuteTimer()
{
float nextTrigger = 0f;
var stopwatch = new Stopwatch();
stopwatch.Start();
while(_isRunning)
{
nextTrigger += _interval;
float elapsed;
while(true)
{
elapsed = ElapsedHiRes(stopwatch);
float diff = nextTrigger - elapsed;
if(diff <= 0f)
break;
if(diff < 1f)
Thread.SpinWait(10);
else if(diff < 5f)
Thread.SpinWait(100);
else if(diff < 15f)
Thread.Sleep(1);
else
Thread.Sleep(10);
if(!_isRunning)
return;
}
float delay = elapsed - nextTrigger;
Elapsed?.Invoke(this, new HiResTimerElapsedEventArgs(delay));
if(!(stopwatch.Elapsed.TotalHours >= 1d))
continue;
stopwatch.Restart();
nextTrigger = 0f;
}
stopwatch.Stop();
}
static float ElapsedHiRes(Stopwatch stopwatch) => stopwatch.ElapsedTicks * tickFrequency;
}
}

View File

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