Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress bar in console application

I'm writing a simple c# console app that uploads files to sftp server. However, the amount of files are large. I would like to display either percentage of files uploaded or just the number of files upload already from the total number of files to be upload.

First, I get all the files and the total number of files.

string[] filePath = Directory.GetFiles(path, "*"); totalCount = filePath.Length; 

Then I loop through the file and upload them one by one in foreach loop.

foreach(string file in filePath) {     string FileName = Path.GetFileName(file);     //copy the files     oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory + "/" + FileName);     //Console.WriteLine("Uploading file..." + FileName);     drawTextProgressBar(0, totalCount); } 

In the foreach loop I have a progress bar which I have issues with. It doesn't display properly.

private static void drawTextProgressBar(int progress, int total) {     //draw empty progress bar     Console.CursorLeft = 0;     Console.Write("["); //start     Console.CursorLeft = 32;     Console.Write("]"); //end     Console.CursorLeft = 1;     float onechunk = 30.0f / total;      //draw filled part     int position = 1;     for (int i = 0; i < onechunk * progress; i++)     {         Console.BackgroundColor = ConsoleColor.Gray;         Console.CursorLeft = position++;         Console.Write(" ");     }      //draw unfilled part     for (int i = position; i <= 31 ; i++)     {         Console.BackgroundColor = ConsoleColor.Green;         Console.CursorLeft = position++;         Console.Write(" ");     }      //draw totals     Console.CursorLeft = 35;     Console.BackgroundColor = ConsoleColor.Black;     Console.Write(progress.ToString() + " of " + total.ToString() + "    "); //blanks at the end remove any excess } 

The output is just [ ] 0 out of 1943

What am I doing wrong here?

EDIT:

I'm trying to display the progress bar while I'm loading and exporting XML files. However, it's going through a loop. After it finishes the first round it goes to the second and so on.

string[] xmlFilePath = Directory.GetFiles(xmlFullpath, "*.xml"); Console.WriteLine("Loading XML files..."); foreach (string file in xmlFilePath) {      for (int i = 0; i < xmlFilePath.Length; i++)      {           //ExportXml(file, styleSheet);           drawTextProgressBar(i, xmlCount);           count++;      }  } 

It never leaves the for loop...Any suggestions?

like image 572
smr5 Avatar asked Jul 23 '14 19:07

smr5


People also ask

What does a progress bar show?

A progress bar is a graphical control element used to visualize the progression of an extended computer operation, such as a download, file transfer, or installation. Sometimes, the graphic is accompanied by a textual representation of the progress in a percent format.

Which bar display the progress bar?

In this example, modeless progress is shown in the address bar. Otherwise, if the window has a status bar, display the modeless progress in the status bar. Put any corresponding text to its left in the status bar. In this example, modeless progress is shown in the status bar.


1 Answers

I was also looking for a console progress bar. I didn't find one that did what I needed, so I decided to roll my own. Click here for the source code (MIT License).

Animated progress bar

Features:

  • Works with redirected output

    If you redirect the output of a console application (e.g., Program.exe > myfile.txt), most implementations will crash with an exception. That's because Console.CursorLeft and Console.SetCursorPosition() don't support redirected output.

  • Implements IProgress<double>

    This allows you to use the progress bar with async operations that report a progress in the range of [0..1].

  • Thread-safe

  • Fast

    The Console class is notorious for its abysmal performance. Too many calls to it, and your application slows down. This class performs only 8 calls per second, no matter how often you report a progress update.

Use it like this:

Console.Write("Performing some task... "); using (var progress = new ProgressBar()) {     for (int i = 0; i <= 100; i++) {         progress.Report((double) i / 100);         Thread.Sleep(20);     } } Console.WriteLine("Done."); 
like image 72
Daniel Wolf Avatar answered Sep 22 '22 22:09

Daniel Wolf