Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# progress bar timer

// ---	ProgressBar
//PUT THIS IN YOUR PROGRAM.CS FILE
using System.Text;

/// <summary>
/// An ASCII progress bar
/// </summary>
public class ProgressBar : IDisposable, IProgress<double>
{
    private const int blockCount = 10;
    private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8);
    private const string animation = @"|/-";

    private readonly Timer timer;

    private double currentProgress = 0;
    private string currentText = string.Empty;
    private bool disposed = false;
    private int animationIndex = 0;

    public ProgressBar()
    {
        timer = new Timer(TimerHandler);

        // A progress bar is only for temporary display in a console window.
        // If the console output is redirected to a file, draw nothing.
        // Otherwise, we'll end up with a lot of garbage in the target file.
        if (!Console.IsOutputRedirected)
        {
            ResetTimer();
        }
    }

    public void Report(double value)
    {
        // Make sure value is in [0..1] range
        value = Math.Max(0, Math.Min(1, value));
        Interlocked.Exchange(ref currentProgress, value);
    }

    private void TimerHandler(object state)
    {
        lock (timer)
        {
            if (disposed) return;

            int progressBlockCount = (int)(currentProgress * blockCount);
            int percent = (int)(currentProgress * 100);
            string text = string.Format("[{0}{1}] {2,3}% {3}",
                new string('■', progressBlockCount), new string(' ', blockCount - progressBlockCount),
                percent,
                animation[animationIndex++ % animation.Length]);
            UpdateText(text);

            ResetTimer();
        }
    }

    private void UpdateText(string text)
    {
        // Get length of common portion
        int commonPrefixLength = 0;
        int commonLength = Math.Min(currentText.Length, text.Length);
        while (commonPrefixLength < commonLength && text[commonPrefixLength] == currentText[commonPrefixLength])
        {
            commonPrefixLength++;
        }

        // Backtrack to the first differing character
        StringBuilder outputBuilder = new StringBuilder();
        outputBuilder.Append('', currentText.Length - commonPrefixLength);

        // Output new suffix
        outputBuilder.Append(text.Substring(commonPrefixLength));

        // If the new text is shorter than the old one: delete overlapping characters
        int overlapCount = currentText.Length - text.Length;
        if (overlapCount > 0)
        {
            outputBuilder.Append(' ', overlapCount);
            outputBuilder.Append('', overlapCount);
        }

        Console.Write(outputBuilder);
        currentText = text;
    }

    private void ResetTimer()
    {
        timer.Change(animationInterval, TimeSpan.FromMilliseconds(-1));
    }

    public void Dispose()
    {
        lock (timer)
        {
            disposed = true;
            UpdateText(string.Empty);
        }
    }

}


//------------ PUT THIS IN A NEW CODE FILE

static class Program
{

    static void Main()
    {
        int min = 600;
        Console.WriteLine("How many minutes do you want your timer to be?");
        string minmultistring= Console.ReadLine();
        int multiplier = Int32.Parse(minmultistring);
        int timer = min * multiplier;
        Console.WriteLine($"Setting a timer for {multiplier} minutes...");
        Thread.Sleep(2000);
        Console.Clear();
        using (var progress = new ProgressBar())
        {
            for (int i = 0; i <= 100; i++)
            {
                progress.Report((double)i / 100);
                Thread.Sleep(timer);
            }
        }
        Console.WriteLine("Done.");
    }

}

Comment

PREVIOUS NEXT
Code Example
Csharp :: c# split quotation 
Csharp :: set request size c# 
Csharp :: windows forms webbrowser refresh 
Csharp :: unity find all scriptable objects of a type 
Csharp :: c# loop through queue 
Csharp :: unity create a textbox in inspector 
Csharp :: minimum of three numbers 
Csharp :: how to get length of okobjectresult c# 
Csharp :: #grid 
Csharp :: Get location in Xamarin - NAYCode.com 
Csharp :: c# get executing file name 
Csharp :: cursor position c# 
Csharp :: how to set the current user httpcontext.current.user asp.net -mvc 
Csharp :: DataGridView ComboBox column selection changed event 
Csharp :: HtmlToPdfConverter 
Csharp :: faucongz 
Csharp :: c# get private property 
Csharp :: asp.net core update-database specify environment 
Csharp :: c# list object 
Csharp :: c# for loops 
Csharp :: open project in visual studio using command prompt 
Csharp :: how to make a cast in c# 
Csharp :: strong email validation regex c# 
Csharp :: c# sort llist 
Csharp :: assert.equal 
Csharp :: get camera position unity 
Csharp :: unity fast sin 
Csharp :: how to c# 
Csharp :: function to accept interger 
Csharp :: dapper extension 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =