国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#基于TimeSpan實現(xiàn)倒計時效果的方法

2020-01-24 02:36:13
字體:
供稿:網(wǎng)友

本文實例展示了C#基于TimeSpan實現(xiàn)倒計時效果的方法,比較實用的功能,對于初學(xué)者來說有一定的學(xué)習(xí)參考價值。具體實現(xiàn)方法如下:

示例代碼如下:

using System;using System.Threading;namespace ConsoleApplication29{  class Program  {    static void Main(string[] args)    {      try      {        DateTime _timeEnd = DateTime.Now.AddSeconds(62);        ThreadPool.QueueUserWorkItem((arg) =>        {          TimeSpan _ts = _timeEnd - DateTime.Now;          while (true)          {            Thread.Sleep(1000);            if (_ts.TotalSeconds >= 0)            {              Console.WriteLine("還剩余{0}分鐘{1}秒", _ts.Minutes, _ts.Seconds);              _ts = _ts.AddSeconds(-1);            }          }        });      }      catch (Exception ex)      {        Console.WriteLine(ex.Message);      }      finally      {        Console.ReadLine();      }    }  }  public static class TimeSpanToolV2  {    public static TimeSpan AddSeconds(this TimeSpan ts, int seconds)    {      return ts.Add(new TimeSpan(0, 0, seconds));    }    public static TimeSpan AddMinutes(this TimeSpan ts, int minutes)    {      return ts.Add(new TimeSpan(0, minutes, 0));    }    public static TimeSpan AddHours(this TimeSpan ts, int hours)    {      return ts.Add(new TimeSpan(hours, 0, 0));    }  }}

代碼運行效果如下:

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大悟县| 和平县| 阿克| 清流县| 桐城市| 上栗县| 灯塔市| 武冈市| 长子县| 西峡县| 柘荣县| 开化县| 青神县| 嵩明县| 安仁县| 固镇县| 徐水县| 固阳县| 静乐县| 元阳县| 鄱阳县| 都兰县| 高安市| 嘉黎县| 博客| 博兴县| 囊谦县| 昌吉市| 龙游县| 浏阳市| 镇原县| 和硕县| 陕西省| 荆州市| 石城县| 礼泉县| 佛教| 唐山市| 那曲县| 贺兰县| 巍山|