几乎就是在讽刺我这类没有事情的人,哈哈哈
窗体的倒计时相称的没有技术含量,主如果不够炫酷,不能够表现我们程序员的身份。
那什么才叫炫酷?必需是掌握台啊!
电视剧上黑客噼里啪啦噼里啪啦滚屏的画面多炫酷!
所以,研讨了一下怎样在掌握台上计时。
百度到了msdn的老帖子,一切的回覆都是运用线程。那就用线程吧。
重要的计时要领就是运用 Thread.Sleep(1000); 要领,使主线程住手一秒。
然后轮回运用线程挪用传参要领 Thread thread = new Thread(delegate() { GetTime(dt); });
这个要领盘算时候差然后输出时候。
如许就会完成一个bug(比心,专心制造BUG),虽然盘算一下时候,再输出,基本上是用不了若干时候的,
然则几百次几千次今后就会致使两次输出时候里少了一秒,但关于末了的效果来讲并没有大碍。
(后知后觉,不必线程挪用要领应当也能够,重要照样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 = "Timer"; //掌握台宽度 Console.WindowWidth = 50; //掌握台高度25是最低 Console.BufferHeight = 25; Console.WriteLine("如今的时候是"); Console.WriteLine(DateTime.Now); Console.WriteLine("输入停止时候"); //猎取设定时候 DateTime dt = Convert.ToDateTime(Console.ReadLine()); //猎取最先时候 DateTime dtNow = Convert.ToDateTime(DateTime.Now); //挪用线程猎取时候 while (isEnd == false) { Thread thread = new Thread(delegate() { GetTime(dt); }); thread.Start(); Thread.Sleep(1000); } Console.ReadLine(); } //输出计时剩余时候 static void GetTime(DateTime dt) { //猎取最先时候 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("计时已完成"); isEnd = true; } } } }
Console.ForegroundColor = ConsoleColor.Green; 转变输出字符的色彩为绿色。
Console.SetCursorPosition(8, 5); 是转变光标的位置的语句,代表光标第5行第8个字符,这时候再举行输出就是在这里输出了。
输出的效果依然是原始细致的时候花样,不过稍作修正就会更雅观一点,下面是一些有关时候的输出语句。
取得当前体系时候: DateTime dt = DateTime.Now; Environment.TickCount能够获得“体系启动到如今”的毫秒值 DateTime now = DateTime.Now; Console.WriteLine(now.ToString("yyyy-MM-dd")); //按yyyy-MM-dd花样输出sConsole.WriteLine(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 (毫秒)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()); // 加上一个时候段(注: System.TimeSpan为一个时候段,组织函数以下public TimeSpan(long ticks); // ticks: A time period expressed in 100-nanosecond units.//nanosecond:十亿分之一秒 new TimeSpan(10,000,000) 为一秒public TimeSpan(int hours, int minutes, int seconds);public TimeSpan(int days, int hours, int minutes, int seconds);public TimeSpan(int days, 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()); //2009年11月Console.WriteLine(dt.GetDateTimeFormats('D')[0].ToString()); //2009年11月26日Console.WriteLine(dt.GetDateTimeFormats('D')[1].ToString()); //星期四, 26 十一月, 2009Console.WriteLine(dt.GetDateTimeFormats('D')[2].ToString()); //26 十一月, 2009Console.WriteLine(dt.GetDateTimeFormats('D')[3].ToString()); //星期四 2009 11 26Console.WriteLine(dt.GetDateTimeFormats('M')[0].ToString()); //26 十一月Console.WriteLine(dt.GetDateTimeFormats('f')[0].ToString()); //2009年11月26日 PM 1:29Console.WriteLine(dt.GetDateTimeFormats('g')[0].ToString()); //26/11/2009 PM 1:29Console.WriteLine(dt.GetDateTimeFormats('r')[0].ToString()); //Thu, 26 Nov 2009 13:29:06 GMT(注: 经常使用的日期时候花样: 花样 申明 输出花样 d 精简日期花样 MM/dd/yyyy D 细致日期花样 dddd, MMMM dd, yyyy f 完全花样 (long date + short time) dddd, MMMM dd, yyyy HH:mm F 完全日期时候花样 (long date + long time) dddd, MMMM dd, yyyy HH:mm:ss g 平常花样 (short date + short time) MM/dd/yyyy HH:mm G 平常花样 (short date + long time) MM/dd/yyyy HH:mm:ss m,M 月日花样 MMMM dd s 适中日期时候花样 yyyy-MM-dd HH:mm:ss t 精简时候花样 HH:mm T 细致时候花样 HH:mm:ss ) Console.WriteLine(string.Format("{0:d}", dt)); //28/12/2009Console.WriteLine(string.Format("{0:D}", dt)); //2009年12月28日Console.WriteLine(string.Format("{0:f}", dt)); //2009年12月28日 AM 10:29Console.WriteLine(string.Format("{0:F}", dt)); //2009年12月28日 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)); //28 十二月Console.WriteLine(string.Format("{0:R}", dt)); //Mon, 28 Dec 2009 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)); //2009年12月28日 AM 2:29:18Console.WriteLine(string.Format("{0:Y}", dt)); //2009年12月Console.WriteLine(string.Format("{0:yyyyMMddHHmmssffff}", dt)); //200912281029182047盘算2个日期之间的天数差 DateTime dt1 = Convert.ToDateTime("2007-8-1"); DateTime dt2 = Convert.ToDateTime("2007-8-15"); TimeSpan span = dt2.Subtract(dt1);int dayDiff = span.Days ; 盘算某年某月的天数int days = DateTime.DaysInMonth(2009, 8); days = 31; 给日期增添一天、削减一天 DateTime dt =DateTime.Now; dt.AddDays(1); //增添一天 dt自身并不转变dt.AddDays(-1);//削减一天 dt自身并不转变
以上就是详解C# 掌握台倒计时的细致内容,更多请关注ki4网别的相干文章!