private static void UpdateProgress(Util.ProgressBar progressBar, int complete, int total, TimeSpan elapsed, string prefix)
 {
     lock (progressBar)
     {
         var rate      = total == 0 ? 0 : complete / elapsed.TotalSeconds;
         var remaining = total == 0 || complete == 0 ? TimeSpan.Zero : TimeSpan.FromSeconds((total - complete) / rate);
         progressBar.Fraction = total == 0 ? 1 : (double)complete / total;
         progressBar.Caption  = $"{prefix}{complete} / {total} @ {rate:f0} / s, {remaining.TotalMinutes:f0}:{remaining.Seconds:00} rem";
     }
 }
Exemple #2
0
 public TimerUtil(long total, int maxMeasurement = 1000, int updateInterval = 25)
 {
     _stopwatch = new Stopwatch();
     if (total <= 0)
     {
         throw new Exception("zero total");
     }
     _total          = total.Dump("Total");
     _maxMeasurement = maxMeasurement;
     _updateInterval = updateInterval;
     _progress       = new Util.ProgressBar().Dump("Work");
     _counterDc      = new DumpContainer().Dump("Count");
     _speed          = new DumpContainer("-- ms/item").Dump("Speed");
     _timeLeft       = new DumpContainer("--").Dump("time left estimate");
     _timeDone       = new DumpContainer("--").Dump("time done estimate");
 }
        public static async Task <TResult[]> MonitorProgressAsync <TSource, TResult>(this IEnumerable <TSource> source, Func <TSource, Task <TResult> > taskFactory, string description = null, string prefix = null, int updateEvery = 1, int concurrencyLimit = 1)
        {
            var progressBar = new Util.ProgressBar($"{prefix}Initializing").Dump(description);

            var queue    = new Queue <TSource>(source);
            var total    = queue.Count;
            var complete = 0;

            var stopwatch = Stopwatch.StartNew();

            UpdateProgress(progressBar, complete, total, stopwatch.Elapsed, prefix);

            var results = new List <TResult>();
            var pending = new List <Task <TResult> >();

            while (queue.Count > 0 || pending.Any())
            {
                if (queue.Count > 0)
                {
                    var entry = queue.Dequeue();
                    pending.Add(taskFactory(entry));
                }

                if (pending.Any(x => x.IsCompleted || x.IsFaulted || x.IsCanceled) || pending.Count >= concurrencyLimit)
                {
                    var completed = await Task.WhenAny(pending);

                    results.Add(await completed);
                    pending.Remove(completed);

                    if (Interlocked.Increment(ref complete) % updateEvery == 0)
                    {
                        UpdateProgress(progressBar, complete, total, stopwatch.Elapsed, prefix);
                    }

                    continue;
                }

                await Task.Delay(100);
            }

            UpdateProgress(progressBar, complete, total, stopwatch.Elapsed, prefix);

            return(results.ToArray());
        }
        public static IEnumerable <T> MonitorProgress <T>(this IQueryable <T> source, string description = null, string prefix = null, int updateEvery = 1)
        {
            var complete = 0;
            var total    = source.Count();

            var progressBar = new Util.ProgressBar(prefix).Dump(description);
            var stopwatch   = Stopwatch.StartNew();

            foreach (var item in source)
            {
                yield return(item);

                if (Interlocked.Increment(ref complete) % updateEvery == 0)
                {
                    UpdateProgress(progressBar, complete, total, stopwatch.Elapsed, prefix);
                }
            }

            UpdateProgress(progressBar, complete, total, stopwatch.Elapsed, prefix);
        }
Exemple #5
0
        public ProgressDisplay(Overlay overlay)
        {
            _overlay = overlay;

            _divTitle = new Div();
            _divTitle.SetClass("progress--title");

            _divAnimationPart1 = new Div();
            _divAnimationPart1.SetClass("line");

            _divAnimationPart2 = new Div();
            _divAnimationPart2.SetClass("line");

            _divAnimationPart3 = new Div();
            _divAnimationPart3.SetClass("line");

            _divAnimationBg = new Div();
            _divAnimationBg.SetClass("animation-bg");

            _divAnimation = new Div(_divAnimationPart1, _divAnimationPart2, _divAnimationPart3, _divAnimationBg);
            _divAnimation.SetClass("progress--animation");

            _btnAbort = new Button("Abort", OnAbort);
            _btnAbort.Hide();

            _progressBar = new Util.ProgressBar(false);

            _progressContainer = new DumpContainer(_progressBar);
            _progressContainer.Hide();

            _divWrap = new Div(_divTitle, _divAnimation, _progressContainer, _btnAbort);
            _divWrap.SetClass("progress-container");

            _divContainer = new Div(_divWrap);
            _divContainer.SetClass("progress");
            _divContainer.Hide();

            VisualTree.Add(_divContainer);
        }
Exemple #6
0
 private void Init(string msg)
 {
     prog         = new Util.ProgressBar(msg).Dump();
     prog.Percent = 0;
 }