C # console output progress and percentage of instance code

Source: Internet
Author: User

Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace ConsoleApplication1
Class Program
Static void Main (string [] args)
Bool isBreak = false;
Lelecolor colorBack = Console. BackgroundColor;
Lelecolor colorFore = Console. ForegroundColor;

// The first line of information
Console. WriteLine ("******* now working ...******");

// Draw the progress bar background in the second row
Console. BackgroundColor = ConsoleColor. DarkCyan;
For (int I = 0; ++ I <= 25 ;)
Console. Write ("");
Console. WriteLine ("");
Console. BackgroundColor = colorBack;

// Output progress in the third row
Console. WriteLine ("0% ");
// Output a prompt in the fourth row. Press enter to cancel the current progress.
Console. WriteLine ("<Press Enter To Break.> ");
// ----------------------- A complete work area is drawn on the top.

// Start to control the progress bar and progress changes
For (int I = 0; ++ I <= 100 ;)
// Check whether a key request exists. If yes, check whether it is a return key. If yes, exit the loop.
If (Console. KeyAvailable & System. Console. ReadKey (true). Key = lelekey. Enter)
IsBreak = true; break;
// Draw the progress bar
Console. BackgroundColor = lelecolor. Yellow; // you can specify the progress bar color.
Console. SetCursorPosition (I/4, 1); // set the cursor position. The parameter is the column number and the row number.
Console. Write (""); // move the progress bar
Console. BackgroundColor = colorBack; // restore the output color
// Update Progress percentage. The principle is the same as above.
Console. ForegroundColor = ConsoleColor. Green;
Console. SetCursorPosition (0, 2 );
Console. Write ("{0 }%", I );
Console. ForegroundColor = colorFore;
// Simulate the delay in actual work; otherwise, the progress is too fast.
System. Threading. Thread. Sleep (100 );
// After the work is completed, the information is output based on the actual situation, and the information indicating exit is clearly displayed.
Console. SetCursorPosition (0, 3 );
Console. Write (isBreak? "Break !!! ":" Finished .");
Console. WriteLine ("");
// Wait for exit
Console. ReadKey (true );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.