Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
C# Process Timer Precision Calculation of Items or Bytes per Second.
public class ProcessTimer
{
public enum TimerTypes
{
FormsTimer,
MediaTimer
}
private readonly Stopwatch _intervalTimer;
private readonly MMTimer _mmtimer;
private readonly Timer _fmtimer;
private readonly TimerTypes _timertype;
private bool _enabled;
private double _holditems;
private double _intervalRatio;
private double _itemsdif;
/// <summary>
/// Maybe used for informational purposes.
/// </summary>
public DateTime StartTime;
/// <summary>
/// Contains any possible deviation from the set interval.
/// </summary>
public long TrueInterval;
/// <summary>
/// The interval and timer type can only be set from within the constructor.
/// </summary>
public ProcessTimer(int interval = 20, TimerTypes tt = TimerTypes.MediaTimer)
{
_timertype = tt;
if (_timertype == TimerTypes.FormsTimer)
{
_fmtimer = new Timer();
_fmtimer.Interval = interval;
_fmtimer.Tick += _timer_Elapsed;
GC.KeepAlive(_fmtimer);
}
else
{
_mmtimer = new MMTimer();
_mmtimer.Interval = interval;
_mmtimer.Elapsed += _mmtimer_Elapsed;
GC.KeepAlive(_mmtimer);
}
_intervalTimer = new Stopwatch();
_intervalRatio = 1000D / interval;
_holditems = 0;
}
/// <summary>
/// Get the percentage complete
/// </summary>
public double Percent
{
get
{
if ((int) TotalItemsCount == 0)
return 0;
var p = ItemsProcessed * 100.0f / TotalItemsCount;
if (p > 100.0)
p = 100;
return p;
}
}
/// <summary>
/// Set before starting
/// </summary>
public double TotalItemsCount { get; set; }
/// <summary>
/// Update as the process runs
/// </summary>
public double ItemsProcessed { get; set; }
/// <summary>
/// Get the items per second calculation.
/// </summary>
public double ItemsPerSecond => _itemsdif * _intervalRatio;
/// <summary>
/// used to trigger a process.report within a IProgress reporting process within an async function.
/// </summary>
public bool Trigger { get; set; }
/// <summary>
/// Enable or Disable this timer.
/// </summary>
public bool Enabled
{
get => _enabled;
set
{
if (_timertype == TimerTypes.FormsTimer)
GC.KeepAlive(_fmtimer);
else GC.KeepAlive(_mmtimer);
_enabled = value;
if (_enabled)
{
if (_timertype == TimerTypes.FormsTimer)
_fmtimer.Start();
else _mmtimer.Start();
_intervalTimer.Restart();
StartTime = DateTime.Now;
}
else
{
if (_timertype == TimerTypes.FormsTimer)
_fmtimer.Stop();
else _mmtimer.Stop();
_intervalTimer.Stop();
StartTime = DateTime.MinValue;
}
}
}
private void _timer_Elapsed(object sender, EventArgs e)
{
_intervalTimer.Stop();
_fmtimer.Enabled = false;
if (_intervalTimer.ElapsedMilliseconds != 0)
{
_intervalRatio = 1000D / _intervalTimer.ElapsedMilliseconds;
TrueInterval = _intervalTimer.ElapsedMilliseconds;
}
else
{
if (_fmtimer.Interval != 0)
_intervalRatio = 1000D / _fmtimer.Interval;
else throw new Exception("Timer interval set to zero.");
}
_itemsdif = ItemsProcessed - _holditems;
_holditems = ItemsProcessed;
Trigger = true;
_fmtimer.Enabled = true;
_intervalTimer.Restart();
}
private void _mmtimer_Elapsed(object sender, EventArgs e)
{
_intervalTimer.Stop();
_mmtimer.Stop();
if (_intervalTimer.ElapsedMilliseconds != 0)
{
_intervalRatio = 1000D / _intervalTimer.ElapsedMilliseconds;
TrueInterval = _intervalTimer.ElapsedMilliseconds;
}
else
{
if (_mmtimer.Interval != 0)
_intervalRatio = 1000D / _mmtimer.Interval;
else throw new Exception("Media Timer interval set to zero.");
}
_itemsdif = ItemsProcessed - _holditems;
_holditems = ItemsProcessed;
Trigger = true;
_mmtimer.Start();
_intervalTimer.Restart();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment