2017-05-28 16:47:21 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-06-12 17:47:31 -04:00
|
|
|
|
namespace SharedLibrary.Helpers
|
2017-05-28 16:47:21 -04:00
|
|
|
|
{
|
|
|
|
|
public sealed class AsyncStatus
|
|
|
|
|
{
|
|
|
|
|
DateTime StartTime;
|
|
|
|
|
int TimesRun;
|
2017-05-28 21:07:33 -04:00
|
|
|
|
int UpdateFrequency;
|
2017-05-28 16:47:21 -04:00
|
|
|
|
public double RunAverage { get; private set; }
|
|
|
|
|
public object Dependant { get; private set; }
|
|
|
|
|
public Task RequestedTask { get; private set; }
|
2018-02-10 01:26:38 -05:00
|
|
|
|
public CancellationTokenSource TokenSrc { get; private set; }
|
2017-05-28 16:47:21 -04:00
|
|
|
|
|
2017-05-28 21:07:33 -04:00
|
|
|
|
public AsyncStatus(object dependant, int frequency)
|
2017-05-28 16:47:21 -04:00
|
|
|
|
{
|
2018-02-10 01:26:38 -05:00
|
|
|
|
TokenSrc = new CancellationTokenSource();
|
2017-05-28 16:47:21 -04:00
|
|
|
|
StartTime = DateTime.Now;
|
|
|
|
|
Dependant = dependant;
|
2017-05-28 21:07:33 -04:00
|
|
|
|
UpdateFrequency = frequency;
|
2017-05-28 16:47:21 -04:00
|
|
|
|
// technically 0 but it's faster than checking for division by 0
|
|
|
|
|
TimesRun = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CancellationToken GetToken()
|
|
|
|
|
{
|
2018-02-10 01:26:38 -05:00
|
|
|
|
return TokenSrc.Token;
|
2017-05-28 16:47:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double ElapsedMillisecondsTime()
|
|
|
|
|
{
|
|
|
|
|
return (DateTime.Now - StartTime).TotalMilliseconds;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 00:19:06 -05:00
|
|
|
|
public void Update(Task<bool> T)
|
2017-05-28 16:47:21 -04:00
|
|
|
|
{
|
2018-02-10 01:26:38 -05:00
|
|
|
|
// reset the token source
|
|
|
|
|
TokenSrc.Dispose();
|
|
|
|
|
TokenSrc = new CancellationTokenSource();
|
|
|
|
|
|
2017-05-28 16:47:21 -04:00
|
|
|
|
RequestedTask = T;
|
2018-02-08 02:23:45 -05:00
|
|
|
|
// Console.WriteLine($"Starting Task {T.Id} ");
|
2017-05-28 16:47:21 -04:00
|
|
|
|
RequestedTask.Start();
|
|
|
|
|
|
2017-05-28 21:07:33 -04:00
|
|
|
|
if (TimesRun > 25)
|
2017-05-28 16:47:21 -04:00
|
|
|
|
TimesRun = 1;
|
|
|
|
|
|
2017-05-28 21:07:33 -04:00
|
|
|
|
RunAverage = RunAverage + ((DateTime.Now - StartTime).TotalMilliseconds - RunAverage - UpdateFrequency) / TimesRun;
|
2017-05-28 16:47:21 -04:00
|
|
|
|
StartTime = DateTime.Now;
|
|
|
|
|
}
|
2018-02-10 23:33:42 -05:00
|
|
|
|
|
|
|
|
|
public void Abort()
|
|
|
|
|
{
|
|
|
|
|
RequestedTask = null;
|
|
|
|
|
}
|
2017-05-28 16:47:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|