The C console outputs instance code for progress and percentages

  • 2020-05-09 19:12:31
  • OfStack


 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

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

             // The first 1 Line information             
             Console.WriteLine("****** now working...******");

             // The first 2 Line draws the progress bar background             
             Console.BackgroundColor = ConsoleColor.DarkCyan;
             for (int i = 0; ++i <= 25; )
             {
                 Console.Write(" ");
             }
             Console.WriteLine(" ");
             Console.BackgroundColor = colorBack;

             // The first 3 Line output progress             
             Console.WriteLine("0%");
             // The first 4 Line output prompt , Press enter to cancel the current schedule             
             Console.WriteLine("<Press Enter To Break.>");
             //----------------------- It's drawn up here 1 A complete work area , So let's get to work 

             // Start controlling the progress bar and progress changes             
             for (int i = 0; ++i <= 100; )
             {
                 // First check if there is a button request , If you have , Determines if it is the enter key , If so, exit the loop                 
                 if (Console.KeyAvailable && System.Console.ReadKey(true).Key == ConsoleKey.Enter)
                 {
                     isBreak = true; break;
                 }
                 // Draw progress bar progress                  
                 Console.BackgroundColor = ConsoleColor.Yellow;// Set the progress bar color                 
                 Console.SetCursorPosition(i / 4, 1);// Set cursor position , The parameters are column number and row number                 
                 Console.Write(" ");// Moving progress bar                 
                 Console.BackgroundColor = colorBack;// Restore output color                 
                 // Update progress percentage , The principle of same .                
                 Console.ForegroundColor = ConsoleColor.Green;
                 Console.SetCursorPosition(0, 2);
                 Console.Write("{0}%", i);
                 Console.ForegroundColor = colorFore;
                 // Simulate delays in real work , Otherwise it will go too fast                 
                 System.Threading.Thread.Sleep(100);
             }
             // Work is done , Output information according to the actual situation , And a clear message to quit             
             Console.SetCursorPosition(0, 3);
             Console.Write(isBreak ? "break!!!" : "finished.");
             Console.WriteLine(" ");
             // Waiting for the exit             
             Console.ReadKey(true);
         }
     }
 }

Related articles: