Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Estimated Time of Completion Calculation - C#

RSS
Modified on Fri, Jun 08, 2018, 9:58 AM by Administrator Categorized as Algorithms

Reusable Code

public class EtcCalculator
{
    private readonly DateTime _startTime;
    private readonly int _imax; // upper bound of the zero-based array being processed

    /// <summary>
    /// 
    /// </summary>
    /// <param name="maxIndex">upper bound of the zero-based array being processed</param>
    public EtcCalculator(int maxIndex)
    {
        _imax = maxIndex;
        _startTime = DateTime.Now;
    }

    public DateTime GetEtc(int iterationsDone)
    {
        var iterationsTotal = _imax + 1;
        var msElapsed = DateTime.Now.Subtract(_startTime).TotalMilliseconds;
        var unitTime = msElapsed / (double)iterationsDone;
        var etc = DateTime.Now.AddMilliseconds(unitTime * (iterationsTotal - iterationsDone));
        return etc;
    }
}

Sample Usage

// TODO: Initialize items List
var i = 0;
var timer = new EtcCalculator(items.Count - 1);
foreach (var item in items)
{
    // TODO: Process item
    i++;
    Console.WriteLine($"ETC = {timer.GetEtc(i)}");
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.