Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
C# Process Time Calulator, Calculate Elapsed, Remaining, and Total Processing Times
public class ProcessTimeCalculator
{
private readonly int _limit = 100;
public double CalculatingThreshold = 5;
public string ElapsedTime = "Calculating...";
public double ElapsedTimeInSeconds;
public bool IncludeMilliSeconds = false;
public bool IncludeSeconds = true;
public string RemainingTime = "Calculating...";
public double RemainingTimeInSeconds;
public string TotalTime = "Calculating...";
public double TotalTimeInSeconds;
private readonly LimitedList<double> totseclst;
public ProcessTimeCalculator(int limit = 100)
{
_limit = limit;
totseclst = new LimitedList<double>(_limit);
}
public string ToTotalTime(double tTime)
{
var tts = "";
try
{
var rts = new TimeSpan(TimeSpan.FromSeconds(tTime).Ticks);
TotalTimeInSeconds = tTime;
tts = $"{rts.Days * 24 + rts.Hours:00}:{rts.Minutes:00}";
if (IncludeSeconds) tts += $":{rts.Seconds:00}";
if (IncludeMilliSeconds) tts += $":{rts.Milliseconds:000}";
}
catch
{
tts = "Error.";
}
return tts;
}
/// <summary>
/// Calculate Times based on current percentage of completion and start time.
/// </summary>
public void CalculateTimes(double Percent, DateTime StartTime)
{
try
{
var elapsedseconds = DateTime.Now.Subtract(StartTime);
var Ratio = 100.0 / Percent;
totseclst.Add(elapsedseconds.TotalSeconds * Ratio);
var TotalSeconds = totseclst.AverageEx();
ElapsedTimeInSeconds = elapsedseconds.TotalSeconds;
var etsr = new TimeSpan(TimeSpan.FromSeconds(elapsedseconds.TotalSeconds).Ticks);
ElapsedTime = $"{etsr.Days * 24 + etsr.Hours:00}:{etsr.Minutes:00}";
if (IncludeSeconds) ElapsedTime += $":{etsr.Seconds:00}";
if (IncludeMilliSeconds) ElapsedTime += $":{etsr.Milliseconds:000}";
if (Percent < CalculatingThreshold) return;
var rts = new TimeSpan(TimeSpan.FromSeconds(TotalSeconds).Ticks);
TotalTimeInSeconds = TotalSeconds;
TotalTime = $"{rts.Days * 24 + rts.Hours:00}:{rts.Minutes:00}";
if (IncludeSeconds) TotalTime += $":{rts.Seconds:00}";
if (IncludeMilliSeconds) TotalTime += $":{rts.Milliseconds:000}";
RemainingTimeInSeconds = TimeSpan.FromSeconds(TotalSeconds).Subtract(elapsedseconds).Seconds;
var rtsr = new TimeSpan(TimeSpan.FromSeconds(TotalSeconds).Subtract(elapsedseconds).Ticks);
RemainingTime = $"{rtsr.Days * 24 + rtsr.Hours:00}:{rtsr.Minutes:00}";
if (IncludeSeconds) RemainingTime += $":{rtsr.Seconds:00}";
if (IncludeMilliSeconds) RemainingTime += $":{rtsr.Milliseconds:000}";
}
catch
{
RemainingTime = "Calculating...";
TotalTime = "Calculating...";
ElapsedTime = "Calculating...";
}
}
/// <summary>
/// Calculate Times based on current percentage of completion and start time contained within the ProcessTimer Class
/// </summary>
public void CalculateTimes(ProcessTimer pt)
{
try
{
var elapsedseconds = DateTime.Now.Subtract(pt.StartTime);
var Ratio = 100.0 / pt.Percent;
totseclst.Add(elapsedseconds.TotalSeconds * Ratio);
var TotalSeconds = totseclst.AverageEx();
if (pt.Percent < CalculatingThreshold) return;
ElapsedTimeInSeconds = elapsedseconds.TotalSeconds;
var etsr = new TimeSpan(TimeSpan.FromSeconds(elapsedseconds.TotalSeconds).Ticks);
ElapsedTime = $"{etsr.Days * 24 + etsr.Hours:00}:{etsr.Minutes:00}";
if (IncludeSeconds) ElapsedTime += $":{etsr.Seconds:00}";
if (IncludeMilliSeconds) ElapsedTime += $":{etsr.Milliseconds:000}";
if (pt.Percent < CalculatingThreshold) return;
var rts = new TimeSpan(TimeSpan.FromSeconds(TotalSeconds).Ticks);
TotalTimeInSeconds = TotalSeconds;
TotalTime = $"{rts.Days * 24 + rts.Hours:00}:{rts.Minutes:00}";
if (IncludeSeconds) TotalTime += $":{rts.Seconds:00}";
if (IncludeMilliSeconds) TotalTime += $":{rts.Milliseconds:000}";
RemainingTimeInSeconds = TimeSpan.FromSeconds(TotalSeconds).Subtract(elapsedseconds).Seconds;
var rtsr = new TimeSpan(TimeSpan.FromSeconds(TotalSeconds).Subtract(elapsedseconds).Ticks);
RemainingTime = $"{rtsr.Days * 24 + rtsr.Hours:00}:{rtsr.Minutes:00}";
if (IncludeSeconds) RemainingTime += $":{rtsr.Seconds:00}";
if (IncludeMilliSeconds) RemainingTime += $":{rtsr.Milliseconds:000}";
}
catch
{
RemainingTime = "Calculating...";
TotalTime = "Calculating...";
ElapsedTime = "Calculating...";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment