bar progress in console C#

josesalaz

New member
Joined
Apr 14, 2023
Messages
4
Programming Experience
Beginner
Hello

I have a code that gets the IdentifyingNumber of a program installed in Windows. However, during the time it gets the info, the console log is empty with no information about the progress of the code. I would need to show a progress bar to show the status of the code progress. At the end, the code prints an output4 = C3BCB0F2-5303-489D-AFAB-218B416D000D. Before, that print I would need to show the progress from 0 to 100 or something.
According to my understanding, one way to do this is to use a percentage-based progress bar. In this case, you would calculate the percentage of work completed by dividing the current amount of work by the total amount of work and multiplying by 100. For example, if you have completed 20 out of 100 tasks, the progress would be 20% (20/100 * 100).
But that's the problem, How do I know the total amount if the total amount is based in the variable CPU process.? I can not predict that value. I have tried with a function that I call but this just a static bar (useless)
I will appreciate any idea
Thanks


Please, follow the code here below:

code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;

namespace test
{
    public class Program
    {
        static void Main(string[] args)
        {

            int progress = 0;
            int max = 100;
            Console.Write(GetProgressBar(progress, max));



            ProcessStartInfo startInfo = new ProcessStartInfo("wmic");
            startInfo.UseShellExecute = false;
            startInfo.Arguments = "product where \"Name like '%Genetec Sipelia%'\" get IdentifyingNumber";
            startInfo.RedirectStandardOutput = true;

            Process process = new Process();
            process.StartInfo = startInfo;
            process.Start();
        

            // Update progress and wait for process to finish
            progress += 25;
            Console.Write(GetProgressBar(progress, max));
            process.WaitForExit();

            // Step 2: Read output and wait
            string output = process.StandardOutput.ReadToEnd();
            Thread.Sleep(1000);

            // Update progress
            progress += 25;
            Console.Write(GetProgressBar(progress, max));


            string input = output;
            string pattern2 = @"IdentifyingNumber\s+";

            string output3 = Regex.Replace(input, pattern2, "");

            string input3 = output3;
            string pattern3 = @"[{}]+";
            string output4 = Regex.Replace(input3, pattern3, "");

            /
          

            Console.WriteLine("the output4 :" + output4);



            Console.ReadLine();

        }

        static string GetProgressBar(int progress, int max)
        {
            int percent = (int)((double)progress / max * 100);
            int numFilled = (int)((double)progress / max * 50);
            string progressBar = $"|{new string('=', numFilled)}{new string(' ', 50 - numFilled)}| {percent}%";
            return progressBar;
        }
    }
}
 
But that's the problem, How do I know the total amount if the total amount is based in the variable CPU process.? I can not predict that value.

If you don't know the number of steps taken (and relative duration of each of those steps), then you are using the wrong UI because you can't really show the progress. Instead you should just show some kind of busy UI. The traditional console way of doing this is just to show a rotating line at a single spot(e.g. |, icode]/[/icode], icode]-[/icode], icode]\[/icode]), or series of dots gets drawn every few seconds.
 
As an after thought, I recall one of my college classmates made a console based "busy indicator" by doing a Knight Rider-like shuttling of characters across the screen. It was pretty impressive on an old VT-52 green screen, and what it made more impressive was he didn't use the curses library, or any ANSI Escape codes. It was done purely with putch()'s and printf()'s.
 
If you don't know the number of steps taken (and relative duration of each of those steps), then you are using the wrong UI because you can't really show the progress. Instead you should just show some kind of busy UI. The traditional console way of doing this is just to show a rotating line at a single spot(e.g. |, icode]/[/icode], icode]-[/icode], icode]\[/icode]), or series of dots gets drawn every few seconds.

could you please elaborate a little bit about how the code will be?
 
How do I know the total amount if the total amount is based in the variable CPU process.?

You don't

Either: do something to work out how much work there is to do before you start doing it
or
Just provide a counter of the work items done so far so the user doesnt think the program crashed
 
Back
Top Bottom