生活随笔
收集整理的這篇文章主要介紹了
时间管理器,时钟
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一個(gè)時(shí)鐘程序,利用線程把計(jì)算時(shí)間走動(dòng),比PC機(jī)中的時(shí)鐘要準(zhǔn)確。
/// <summary>/// 時(shí)間管理器/// </summary>public sealed class TimerManager{private static Timer _Timer;private static bool _IsStarted;private static DateTime _DateTime;static TimerManager(){TimerManager._Timer = null;TimerManager._DateTime = DateTime.Now;TimerManager._IsStarted = false;}/// <summary>/// 檢查時(shí)間管理器是否在正常工作/// </summary>public static bool IsStarted{get{return TimerManager._IsStarted;}}/// <summary>/// 檢查時(shí)間管理器處于靜止中異常!/// </summary>private static void CheckIsNotStarted(){if (!TimerManager._IsStarted){throw new TimerException("時(shí)間管理器處于靜止中異常!");}}/// <summary>/// 檢查時(shí)間管理器處于運(yùn)行中異常!/// </summary>private static void CheckIsStarted(){if (TimerManager._IsStarted){throw new TimerException("時(shí)間管理器處于運(yùn)行中異常!");}}/// <summary>/// 關(guān)閉時(shí)間管理器/// </summary>public static void Dispose(){TimerManager.CheckIsNotStarted();TimerManager._IsStarted = false;TimerManager._Timer.Dispose();}/// <summary>/// 格式化時(shí)間/// </summary>/// <param name="datetime">時(shí)間參數(shù)</param>/// <returns>返回格式后的時(shí)間</returns>private static DateTime FormatDateTime(DateTime datetime){return new DateTime(datetime.Year, datetime.Month, datetime.Day, datetime.Hour, datetime.Minute, datetime.Second, datetime.Kind);}/// <summary>/// 時(shí)間器運(yùn)行回調(diào)函數(shù)/// </summary>/// <param name="state">回調(diào)方法要使用的信息的對(duì)象</param>private static void RunTimer(object state){TimerManager._DateTime = TimerManager._DateTime.AddSeconds(1);}/// <summary>/// 啟動(dòng)時(shí)間管理器/// </summary>/// <param name="datetime">輸入當(dāng)前需要管理的時(shí)間</param>public static void StartManager(DateTime datetime){TimerManager.CheckIsStarted();TimerManager._IsStarted = true;TimerManager._DateTime = TimerManager.FormatDateTime(datetime);TimerManager._Timer = new Timer(new TimerCallback(TimerManager.RunTimer), null, 0x3e7 - datetime.Millisecond, 0x3e8);}/// <summary>/// 獲取當(dāng)前時(shí)間/// </summary>public static DateTime CurrentDateTime{get{TimerManager.CheckIsNotStarted();return TimerManager._DateTime;}}/// <summary>/// 獲取當(dāng)前日子結(jié)束的時(shí)間/// </summary>public static DateTime DayEnd{get{TimerManager.CheckIsNotStarted();return new DateTime(TimerManager._DateTime.Year, TimerManager._DateTime.Month, TimerManager._DateTime.Day, 0x17, 0x3b, 0x3b, TimerManager._DateTime.Kind);}}/// <summary>/// 獲取當(dāng)前日子開始的時(shí)間/// </summary>public static DateTime DayStart{get{TimerManager.CheckIsNotStarted();return new DateTime(TimerManager._DateTime.Year, TimerManager._DateTime.Month, TimerManager._DateTime.Day, 0, 0, 0, TimerManager._DateTime.Kind);}}}/// <summary>/// 時(shí)間管理器錯(cuò)誤異常類型/// </summary>public sealed class TimerException : Exception{/// <summary>/// 時(shí)間軸管理器異常/// </summary>/// <param name="message">錯(cuò)誤信息</param>public TimerException(string message): base(message){}}
總結(jié)
以上是生活随笔為你收集整理的时间管理器,时钟的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。