基於Thread實現本身的定時器Timer

在編程中會常常用到定時器,使用最可能是System.Window.Form下面的Timer控件或者System.Threading下面的Timer,前者須要在winform中使用,然後者提供的功能很簡單,不能沒有提供start和stop形式,即只能調用一次。因此就本身基於Thread實現了一個簡單的定時器,只是用於簡單的耗時少的定時處理任務的狀況。編程

(1)思路ide

建立一個線程,在線程執行函數中,使用一個可控的循環,在循環內執行一個耗時不多的任務,在任務執行完以後,讓線程Sleep規定的時間。函數

(2)代碼測試

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

namespace Wfl.Utility.Common.Tools
{
    /// <summary>
    /// 基於線程實現的定時器
    /// </summary>
    public class ThreadTimer
    {
        /// <summary>
        /// 線程
        /// </summary>
        private Thread _thread;

        /// <summary>
        /// 標識線程是否執行
        /// </summary>
        private volatile bool _isRunning;

        /// <summary>
        /// 定時器的間隔時間
        /// </summary>
        private int _intervalSeconds;

        /// <summary>
        /// 回調函數
        /// </summary>
        private ThreadTimerCallback _callback;

        /// <summary>
        /// 回調函數
        /// </summary>
        private ThreadTimerCallbackParam _callbackParam;

        /// <summary>
        /// 每次線程休息的時間毫秒數
        /// </summary>
        private int _sleepMicseconds;

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="callback"></param>
        public ThreadTimer(ThreadTimerCallback callback,int intervalSeconds)
        {
            _callback = callback;
            _intervalSeconds = intervalSeconds;
            _sleepMicseconds = 100;
        }

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="callbackParam"></param>
        public ThreadTimer(ThreadTimerCallbackParam callbackParam, int intervalSeconds)
        {
            _callbackParam = callbackParam;
            _intervalSeconds = intervalSeconds;
            _sleepMicseconds = 100;
        }

        /// <summary>
        /// 啓動定時器
        /// </summary>
        public void StartTimer()
        {
            _isRunning = true;
            _thread = new Thread(new ThreadStart(IntervalCall));
            _thread.Start();
        }

        /// <summary>
        /// 啓動定時器
        /// </summary>
        /// <param name="objParams"></param>
        public void StartTimer(object[] objParams)
        {
            _isRunning = true;
            _thread = new Thread(new ParameterizedThreadStart(IntervalCall));
            _thread.Start(objParams);
        }

        /// <summary>
        /// 中止定時器
        /// </summary>
        public void StopTimer()
        {
            _isRunning = false;
            try
            {
                _thread.Join();
            }
            catch
            {
            }
        }

        /// <summary>
        /// 線程函數,定時調用回調函數
        /// </summary>
        private void IntervalCall()
        {
            int runTime = _intervalSeconds * 1000;
            int curTime = 0;
            while (_isRunning)
            {
                if (curTime == runTime)
                {
                    curTime = 0;
                    _callback();     //該函數應該很快返回,耗時不多
                }
                else
                {
                    Thread.Sleep(_sleepMicseconds);
                    curTime += _sleepMicseconds;
                }
            }
        }

        /// <summary>
        /// 線程函數,定時調用回調函數
        /// </summary>
        /// <param name="obj"></param>
        private void IntervalCall(object obj)
        {
            object[] objParams = obj as object[];
            int runTime = _intervalSeconds * 1000;
            int curTime = 0;
            while (_isRunning)
            {
                if (curTime == runTime)
                {
                    curTime = 0;
                    _callbackParam(objParams);  //該函數應該很快返回,耗時不多
                }
                else
                {
                    Thread.Sleep(_sleepMicseconds);
                    curTime += _sleepMicseconds;
                }
            }
        }
    }
}
View Code

(3)測試代碼spa

class Program
    {
        static void Main(string[] args)
        {
            ThreadTimer timer = new ThreadTimer(new ThreadTimerCallback(DoWork), 1);
            timer.StartTimer();
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(i);
                Thread.Sleep(1000);
            }
            Console.ReadKey();
            timer.StopTimer();
        }

        static void DoWork()
        {
            DateTime time = DateTime.Now;
            Console.WriteLine(time.ToString());
        }
    }
View Code

(4)截圖線程

相關文章
相關標籤/搜索