TIPS: Give a heartbeat to the boring. Net console Program (character Interface) (about the use of backspace '\ B)
Source: Internet
Author: User
/**/ /*
The vast majority of the sample code of the blog I wrote is the console.Program
There are some tips on the Character interface, which are quite simple. I will extract them again, so I will not talk much nonsense!
They are all skill-related:
Give a heartbeat to the boring. Net console Program (about the use of backspace '\ B')
Same for Java
*/
Public Class Class1
{
Public Static Bool B; // End Condition
Static Void Main ( String [] ARGs)
{
System. Console. writeline ( " Test 1: directly wait for n = 199 cycles " );
Wait ( 199 );
System. Console. writeline ( " \ N Test 2: Waiting For end condition: B = true " );
// Main program starts
B = False ;
New System. Threading. Thread ( New System. Threading. threadstart (dowait). Start (); // Monitoring thread: displays a rolling counter
// The following are time-consuming main programs
System. Threading. thread. Sleep ( 5 * 1000 ); // The main program takes 5 seconds.
B = True ; // Main program ended
System. Console. writeline ( " \ N the main program took 5 seconds " );
}
Private Static Void Wait ( Int Count)
{
System. Console. Write ( " In " );
String BS = "" ; // Used to record the last digit
String S = "" ;
For ( Int I = 0 ; I < Count + 1 ; I ++ )
{
S = System. datetime. Now. tostring ();
System. Threading. thread. Sleep ( 10 ); // 10/1000 second
System. Console. Write (BS + " \ B \ B " + I + " Times, " + S );
BS = New String ( ' \ B ' , Digits (I) + S. Length + 1 ); // 19 is the string length of the date and time, and 1 is ","
}
}
Private Static Void Dowait ()
{
Wait (RefB );//Real Functions called by Delegation
}
Private Static Void Wait ( Ref Bool Flag) // Flag can be modified in other threads
{
System. Console. Write ( " In progress " );
Int I = 0 ;
String BS = "" ;
String S = "" ;
While ( ! Flag)
{
// System. Threading. thread. Sleep (1000 ); // 1 second
S = System. datetime. Now. tostring ();
System. Console. Write (BS + " \ B \ B " + I + " Times, " + S );
BS = New String ( ' \ B ' , Digits (I) + S. Length + 1 ); // 19 is the string length of the date and time, and 1 is ","
I ++ ;
}
}
Static Int Digits ( Int N) // Number of digits
{
N = System. Math. Abs (N );
N = N / 10 ;
Int I = 1 ;
While (N > 0 )
{
N=N/10;
I++;
}
Return I;
}
}
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.