国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學(xué)習(xí)啦 > 學(xué)習(xí)英語 > 專業(yè)英語 > 計(jì)算機(jī)英語 > c中timer的用法

    c中timer的用法

    時(shí)間: 長(zhǎng)思709 分享

    c中timer的用法

      c中timer的用法的用法你知道嗎?下面小編就跟你們?cè)敿?xì)介紹下c中timer的用法的用法,希望對(duì)你們有用。

      c中timer的用法的用法如下:

      關(guān)于C#中timer類 在C#里關(guān)于定時(shí)器類就有3個(gè)

      1.定義在System.Windows.Forms里

      2.定義在System.Threading.Timer類里

      3.定義在System.Timers.Timer類里

      System.Windows.Forms.Timer是應(yīng)用于WinForm中的,它是通過Windows消息機(jī)制實(shí)現(xiàn)的,類似于VB或Delphi中的Timer控件,內(nèi)部使用API SetTimer實(shí)現(xiàn)的。它的主要缺點(diǎn)是計(jì)時(shí)不精確,而且必須有消息循環(huán),Console Application(控制臺(tái)應(yīng)用程序)無法使用。

      System.Timers.Timer和System.Threading.Timer非常類似,它們是通過.NET Thread Pool實(shí)現(xiàn)的,輕量,計(jì)時(shí)精確,對(duì)應(yīng)用程序、消息沒有特別的要求。System.Timers.Timer還可以應(yīng)用于WinForm,完全取代上面的Timer控件。它們的缺點(diǎn)是不支持直接的拖放,需要手工編碼。

      例:

      使用System.Timers.Timer類

      //實(shí)例化Timer類,設(shè)置間隔時(shí)間為10000毫秒;

      System.Timers.Timer t = new System.Timers.Timer(10000);

      //到達(dá)時(shí)間的時(shí)候執(zhí)行事件;

      t.Elapsed += new System.Timers.ElapsedEventHandler(theout);

      t.AutoReset = true;//設(shè)置是執(zhí)行一次(false)還是一直執(zhí)行(true);

      t.Enabled = true;//是否執(zhí)行System.Timers.Timer.Elapsed事件;

      ====================================

      自己寫的一個(gè)用System.Timer類的方法

      復(fù)制代碼 代碼如下:

      public class BF_CheckUpdate

      {

      private static object LockObject = new Object();

      // 定義數(shù)據(jù)檢查Timer

      private static Timer CheckUpdatetimer = new Timer();

      // 檢查更新鎖

      private static int CheckUpDateLock = 0;

      ///

      /// 設(shè)定數(shù)據(jù)檢查Timer參數(shù)

      ///

      internal static void GetTimerStart()

      {

      // 循環(huán)間隔時(shí)間(10分鐘)

      CheckUpdatetimer.Interval = 600000;

      // 允許Timer執(zhí)行

      CheckUpdatetimer.Enabled = true;

      // 定義回調(diào)

      CheckUpdatetimer.Elapsed += new ElapsedEventHandler(CheckUpdatetimer_Elapsed);

      // 定義多次循環(huán)

      CheckUpdatetimer.AutoReset = true;

      }

      ///

      /// timer事件

      ///

      ///

      ///

      private static void CheckUpdatetimer_Elapsed(object sender, ElapsedEventArgs e)

      {

      // 加鎖檢查更新鎖

      lock (LockObject)

      {

      if (CheckUpDateLock == 0) CheckUpDateLock = 1;

      else return;

      }

      //More code goes here.

      //具體實(shí)現(xiàn)功能的方法

      Check();

      // 解鎖更新檢查鎖

      lock (LockObject)

      {

      CheckUpDateLock = 0;

      }

      }

      }

    熱門文章

    542974