andrew4582

ProgressEnumerator

Jul 22nd, 2011
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Diagnostics;
  7.  
  8. namespace Core
  9. {
  10.     public class ProgressEnumerator<T>: IEnumerable<T>, IEnumerator<T>, IProgressInfo
  11.     {
  12.         DateTime start = DateTime.UtcNow;
  13.  
  14.         int count;
  15.         int current = 0;
  16.  
  17.         IEnumerator<T> enumerator;
  18.      
  19.         public ProgressEnumerator(IEnumerable<T> source, int count)
  20.         {
  21.             enumerator = source.GetEnumerator();
  22.             this.count = count;
  23.         }
  24.  
  25.         public IEnumerator<T> GetEnumerator()
  26.         {
  27.             return this;
  28.         }
  29.        
  30.         IEnumerator IEnumerable.GetEnumerator()
  31.         {
  32.             return this;
  33.         }
  34.  
  35.         public T Current
  36.         {
  37.             get { return enumerator.Current; }
  38.         }
  39.  
  40.         object IEnumerator.Current
  41.         {
  42.             get { return current; }
  43.         }
  44.  
  45.         public bool MoveNext()
  46.         {
  47.             if (enumerator.MoveNext())
  48.             {
  49.                 current++;
  50.  
  51.                 return true;
  52.             }
  53.             return false;
  54.         }
  55.  
  56.         public void Reset()
  57.         {
  58.             throw new NotImplementedException();
  59.         }
  60.  
  61.         public void Dispose()
  62.         {
  63.  
  64.         }
  65.  
  66.         double IProgressInfo.Percentage
  67.         {
  68.             get { return 100 * ((IProgressInfo)this).Ratio; }
  69.         }
  70.  
  71.         double IProgressInfo.Ratio
  72.         {
  73.             get { return count == 0 ? 0 : current  / (double)count; }
  74.         }
  75.  
  76.         TimeSpan IProgressInfo.Elapsed
  77.         {
  78.             get { return DateTime.UtcNow - start; }
  79.         }
  80.  
  81.         TimeSpan IProgressInfo.Remaining
  82.         {
  83.             get
  84.             {
  85.                 double ratio = ((IProgressInfo)this).Ratio;
  86.                 return ratio == 0 ? TimeSpan.Zero : new TimeSpan((long)((((IProgressInfo)this).Elapsed.Ticks / ratio) * (1 - ratio)));
  87.             }
  88.         }
  89.  
  90.         DateTime IProgressInfo.EstimatedFinish
  91.         {
  92.             get { return (DateTime.UtcNow + ((IProgressInfo)this).Remaining).ToLocalTime(); }
  93.         }
  94.  
  95.         public override string ToString()
  96.         {
  97.             IProgressInfo me = (IProgressInfo)this;
  98.             TimeSpan ts = me.Remaining;
  99.             return "{0:0.00}% | {1}h {2:D2}m {3:D2}s -> {4}".Formato(me.Percentage, ts.Hours, ts.Minutes, ts.Seconds, me.EstimatedFinish);
  100.         }
  101.     }
  102.  
  103.     public interface IProgressInfo
  104.     {
  105.         double Percentage { get; }
  106.  
  107.         double Ratio { get; }
  108.  
  109.         TimeSpan Elapsed { get; }
  110.  
  111.         TimeSpan Remaining { get; }
  112.         DateTime EstimatedFinish { get; }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment