Mpdreamz / shellprogressbar

ShellProgressBar - display progress in your console application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Run progressbar as background Thread.Task

PayteR opened this issue · comments

Hi, how it's possible to run progress bar inside of separated thread? I don't want to code this synchronously, because when i have task longer than 1 sec, then time is not updated until next Tick() is called.

Here is my wrapper class, that run Tick in while loop and it will update every 1/4 sec. Even when this task runs and Tick is called progress bar not updating. What do i do wrong? Thx.

using ShellProgressBar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace App.Infrastructure.Helpers
{
    class ProgressBarHelper
    {
        public virtual int ItemsCount { get; set; } = 0;
        public virtual int CurrentItem { get; set; } = 0;
        public virtual string Message { get; set; } = "";

        public ProgressBarHelper(int itemsCount)
        {
            ItemsCount = itemsCount;

            var options = new ProgressBarOptions
            {
                ProgressCharacter = '\u2588',
                ProgressBarOnBottom = true,
                ForegroundColor = ConsoleColor.White,
                ForegroundColorDone = ConsoleColor.White,
                BackgroundColor = ConsoleColor.DarkGray,
                BackgroundCharacter = '\u2593',
                DisplayTimeInRealTime = false
            };


            using (var pbar = new ProgressBar(ItemsCount, Message, options))
            {
                Task.Run(() =>
                {
                    while (CurrentItem < ItemsCount)
                    {
                        pbar.Tick(CurrentItem, Message);
                        Thread.Sleep(250);
                    }
                });
            }
        }

        public void Stop()
        {
            CurrentItem = ItemsCount;
        }

        public void Next(string message = null)
        {
            if(message != null)
            {
                Message = message;
            }
            CurrentItem++;
        }

        public void SetCurrent(int currentItem, string message = null)
        {
            if(message != null)
            {
                Message = message;
            }
            CurrentItem = currentItem;
        }
    }
}

Hm, no i tried to remove using and now it works. Why do i need to even use using?

public ProgressBarHelper(int itemsCount)
        {
            ItemsCount = itemsCount;


            var options = new ProgressBarOptions
            {
                ProgressCharacter = '\u2588',
                ProgressBarOnBottom = true,
                ForegroundColor = ConsoleColor.White,
                ForegroundColorDone = ConsoleColor.White,
                BackgroundColor = ConsoleColor.DarkGray,
                BackgroundCharacter = '\u2593',
                DisplayTimeInRealTime = false
            };


            var pbar = new ProgressBar(ItemsCount, Message, options);
            Task.Run(() =>
            {
                while (CurrentItem < ItemsCount)
                {
                    pbar.Tick(CurrentItem, Message);
                    Thread.Sleep(250);
                }
            });
        }

DisplayTimeInRealTime = true will update the progressbar every 500ms in background thread already.

In your first example pbar gets disposed but a reference is used in thread delegate.