SoFunction
Updated on 2025-04-06

Example code for output progress and percentage of C# console


 using System;
 using ;
 using ;
 using ;

 namespace ConsoleApplication1
 {
     class Program
     {
         static void Main(string[] args)
         {
             bool isBreak = false;
             ConsoleColor colorBack = ;
             ConsoleColor colorFore = ;

//First line of information
             ("****** now working...******");

//Draw the progress bar background in the second line
             = ;
             for (int i = 0; ++i <= 25; )
             {
                 (" ");
             }
             (" ");
             = colorBack;

//The third line output progress
             ("0%");
//The fourth line output prompt, press Enter to cancel the current progress
             ("<Press Enter To Break.>");
//-----------------------------------------------------------------------------------------------------------------------------

//Start control the progress bar and progress changes
             for (int i = 0; ++i <= 100; )
             {
//First check whether there is a key request. If so, determine whether it is the Enter key. If so, exit the loop.
                 if ( && (true).Key == )
                 {
                     isBreak = true; break;
                 }
//Draw progress bar progress
= ;//Set the progress bar color
(i / 4, 1);//Set the cursor position, which column and row are the parameters.
(" ");//Movement progress bar
= colorBack;//Restore the output color
//The percentage of update progress, the principle is the same as above.
                 = ;
                 (0, 2);
                 ("{0}%", i);
                 = colorFore;
//Simulate delays in actual work, otherwise the progress will be too fast.
                 (100);
             }
//The work is completed, the information is output according to the actual situation, and the information to exit is clearly prompted.
             (0, 3);
             (isBreak ? "break!!!" : "finished.");
             (" ");
//Waiting to exit
             (true);
         }
     }
 }