Detailed C # console Countdown

Source: Internet
Author: User
Tags thread stop
People who used to chat with each other a few years ago wrote a countdown to the form to calculate off-hours and vacation time:)

It's just a mockery of my job-free, hahaha.

The countdown of the form is quite without technical content, mainly not cool enough to reflect the identity of our programmers.

Then what's cool? Must be the console!

The screen of the hacker crackling and rolling screen of the TV show how cool!

So, we studied how to time on the control table.

Baidu to the old MSDN post, all the answers are the use of threads. Then use the thread.

The main timing method is to use Thread.Sleep (1000); method to make the main thread stop for one second.

It then loops through the thread call pass method, thread thread = new Thread (delegate () {GetTime (dt);});

This method calculates the time difference and then outputs the times.

This will complete a bug (than the heart, the intention to create a bug), although the calculation of time, then output, is basically not much time,

But hundreds of times thousands of times will result in two times less than a second output time, but for the final result is not a major hindrance.

(after knowledge, no thread call method should also be possible, mainly thread.sleep (1000);)

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;namespace        Timer{class Program {static bool Isend = FALSE;            static void Main (string[] args) {//console title Console.title = "Timer";            Console width console.windowwidth = 50;            Console height 25 is the lowest console.bufferheight = 25;            Console.WriteLine ("The Time is Now");            Console.WriteLine (DateTime.Now);            Console.WriteLine ("Input up to date");            Gets the set time datetime dt = Convert.todatetime (Console.ReadLine ());            Gets the start time datetime dtnow = Convert.todatetime (DateTime.Now); The calling thread gets the time while (isend = = false) {thread thread = new Thread (delegate () {GetTime (d T);                }); Thread.                Start ();            Thread.Sleep (1000);        } console.readline (); }//Output timing remaining time static void GetTime (DatetIME dt) {//Get start time DateTime Dtnow = Convert.todatetime (DateTime.Now);            TimeSpan ts = Dt-dtnow;            Console.foregroundcolor = Consolecolor.green;            Console.setcursorposition (8, 5);            Console.WriteLine (TS); if (TS.                TotalSeconds <= 1) {console.setcursorposition (0, 9);                Console.WriteLine ("Timing completed");            Isend = true; }        }    }}

Console.foregroundcolor = Consolecolor.green; Change the color of the output character to green.

Console.setcursorposition (8, 5); Is the statement that changes the position of the cursor, which represents the 8th character of the 5th line of the cursor, at which time the output is output here.

The result of the output is still the original detailed time format, but a little modification will be more beautiful, and here are some output statements about time.

Get current system time: DateTime dt = DateTime.Now; Environment.tickcount can get the "system boot to Present" millisecond value DateTime now = DateTime.Now; Console.WriteLine (now.     ToString ("Yyyy-mm-dd")); Output sconsole.writeline in YYYY-MM-DD format (dt.     ToString ()); 26/11/2009 AM 11:21:30console.writeline (dt.         Year.tostring ()); 2009console.writeline (dt.         Date.tostring ()); 26/11/2009 AM 12:00:00console.writeline (dt.     Dayofweek.tostring ()); Thursdayconsole.writeline (dt.     Dayofyear.tostring ()); 330console.writeline (dt.         Hour.tostring ());     11console.writeline (Dt.Millisecond.ToString ()); 801 (ms) Console.WriteLine (dt.     Minute.tostring ()); 21console.writeline (dt.     Month.tostring ()); 11console.writeline (dt.     Second.tostring ()); 30console.writeline (dt. Timeofday.tostring ()); 12:29:51.5181524console.writeline (dt. ToString ()); 26/11/2009 PM 12:29:51console.writeline (dt. AddYears (1). ToString ()); 26/11/2010 PM 12:29:51console.writeline (dt.compareto (DT). ToString ()); 0console.writeline (dT.add (New TimeSpan (1,0,0,0)). ToString ()); Add a time period (note: System.TimeSpan is a time period, the constructor is the following public TimeSpan (long ticks); Ticks:a time period expressed in 100-nanosecond Units.//nanosecond: One-zero seconds New TimeSpan (10,000,000) is a second public timespan ( int hours, int minutes, int seconds);p ublic TimeSpan (int days, int hours, int minutes, int seconds);p ublic TimeSpan (int da ys, int hours, int minutes, int seconds, int milliseconds), Console.WriteLine ("The time is {0}", End-start); Console.WriteLine (dt. Getdatetimeformats (' s ') [0]. ToString ()); 2009-11-26t13:29:06console.writeline (dt. Getdatetimeformats (' t ') [0]. ToString ()); PM 1:29console.writeline (dt. Getdatetimeformats (' y ') [0]. ToString ()); November 2009 Console.WriteLine (dt. Getdatetimeformats (' D ') [0]. ToString ()); November 26, 2009 Console.WriteLine (dt. Getdatetimeformats (' D ') [1]. ToString ()); Thursday, 261 months, 2009console.writeline (dt. Getdatetimeformats (' D ') [2]. ToString ()); 261 months, 2009console.writeline (dt. Getdatetimeformats (' D ') [3]. ToString ()); Week 42,009 26Console.WriteLine (dt. Getdatetimeformats (' M ') [0]. ToString ()); 261 months Console.WriteLine (dt. Getdatetimeformats (' f ') [0]. ToString ()); November 26, 2009 PM 1:29console.writeline (dt. Getdatetimeformats (' g ') [0]. ToString ()); 26/11/2009 PM 1:29console.writeline (dt. Getdatetimeformats (' R ') [0]. ToString ()); Thu, 13:29:06 GMT (Note: Common DateTime format: Format description output format D Thin date format mm/dd/yyyyd verbose date format dddd, MMMM DD, YYYYF full format (long date + s Hort time) dddd, MMMM dd, yyyy hh:mmf full datetime format (long date + long time) dddd, MMMM dd, yyyy HH:MM:SSG General format (short date + sh ORT time) mm/dd/yyyy HH:MMG General format (short date + long time) mm/dd/yyyy hh:mm:ssm,m month-day format MMMM DDS Moderate datetime format YYYY-MM-DD hh:mm:s St Lite time format hh:mmt verbose time format HH:mm:ss) Console.WriteLine (String. Format ("{0:d}", DT)); 28/12/2009console.writeline (String. Format ("{0:d}", DT)); December 28, 2009 Console.WriteLine (String. Format ("{0:f}", DT)); December 28, 2009 AM 10:29console.writeline (String. Format ("{0:f}", DT)); December 28, 2009 AM 10:29:18console.writeline (String. Format ("{0:g}", DT)); 28/12/2009 AM 10:29console.writeline (string. Format ("{0:g}", DT)); 28/12/2009 AM 10:29:18console.writeline (string. Format ("{0:m}", DT)); 282 months Console.WriteLine (string. Format ("{0:r}", DT)); Mon, Dec 10:29:18 gmtconsole.writeline (string. Format ("{0:s}", DT)); 2009-12-28t10:29:18console.writeline (String. Format ("{0:t}", DT)); AM 10:29console.writeline (String. Format ("{0:t}", DT)); AM 10:29:18console.writeline (String. Format ("{0:u}", DT)); 2009-12-28 10:29:18zconsole.writeline (String. Format ("{0:u}", DT)); December 28, 2009 AM 2:29:18console.writeline (String. Format ("{0:y}", DT)); December 2009 Console.WriteLine (String. Format ("{0:yyyymmddhhmmssffff}", DT)); 200912281029182047 calculates the difference in the number of days between 2 dates DateTime dt1 = convert.todatetime ("2007-8-1");D atetime DT2 = Convert.todatetime (" 2007-8-15 "); TimeSpan span = dt2. Subtract (DT1); int daydiff = span. Days; Calculates the number of months in a certain year int day = Datetime.daysinmonth (8);d ays = 31; Adds a date to the dates, reducing the day of the DateTime DT =DATETIME.NOW;DT. AddDays (1); Adding a day DT itself does not change dt. AddDays (-1);//reduction of the day DT itselfdoes not change 
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.