C#-----定時器的幾種實現


   1. System.Windows.Forms.Timer

     計時器最宜用於 Windows 窗體應用程序中,並且必須在窗口中使用,適用於單線程環境,

在此環境中, UI 線程用於執行處理。 它要求用戶代碼提供 UI 消息泵, 並且始終從同一線程操作, 或將調用封送到

其他線程。Windows 窗體計時器組件是單線程的, 且限制為55毫秒的准確度,准確性不高

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestTimer
{
    public partial class frmTimerDemo : Form
    {
        private System.Windows.Forms.Timer timerGetTime;
        public frmTimerDemo()
        {
            InitializeComponent();
        }

        private void frmTimerDemo_Load(object sender, EventArgs e)
        {
            //創建定時器
            timerGetTime = new System.Windows.Forms.Timer();
            //設置定時器屬性
            timerGetTime.Tick+=new EventHandler(HandleTime);
            timerGetTime.Interval = 1000;
            timerGetTime.Enabled = true;
            //開啟定時器
            timerGetTime.Start();
        }

        public void HandleTime(Object myObject, EventArgs myEventArgs)
        {
            labelTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void frmTimerDemo_FormClosed(object sender, FormClosedEventArgs e)
        {
            //停止定時器
            timerGetTime.Stop();
        }
    }
}

   2. System.Timers.Timer

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace TimerDemo
{
    public partial class frmTimerDemo : Form
    {
        private static System.Timers.Timer aTimer;
        public frmTimerDemo()
        {
            InitializeComponent();
        }

        private void frmTimerDemo_Load(object sender, EventArgs e)
        {
            aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 1000;
            aTimer.Enabled = true;
            aTimer.Start();
        }

        public void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            this.labelTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void frmTimerDemo_FormClosed(object sender, FormClosedEventArgs e)
        {
            aTimer.Stop();
        }
    }
}

   3. System.Threading.Timer

     線程計時器也不依賴窗體,是一種簡單的、輕量級計時器,它使用回調方法而不是使用事件,並由線程池線程提供支持

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TimerDemo
{
    class Program
    {
        int TimesCalled = 0;

        void Display(object state)
        {
            Console.WriteLine("{0} {1} keep running.", (string)state, ++TimesCalled);
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            //2秒后第一次調用,每1秒調用一次
            System.Threading.Timer myTimer = new System.Threading.Timer(p.Display, "Processing timer event", 2000, 1000);
            Console.WriteLine("Timer started.");
            Console.ReadLine();
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM