System.Timers.Timer 與 System.Threading.Timer 小間隔

來源:互聯網
上載者:User
設計一個每隔20ms檢查一次狀態的程式,用System.Timers.Timer做測試時發現,前幾次執行timer調用函數的時間相同(把間隔改到1s以上時無此問題),用lock也用(可能是我不太會用lock)。
改為System.Threading.Timer測試發現:
Threading和Timers的timer在小間隔時都存在此問題,分析後初步判斷是初次運行前的間隔時間的問題。
Timers的無法設定初次啟動前間隔所以設定20ms間隔時第一次進入前的間隔也是20ms
Threading的可以設定初次啟動前的間隔,設定較大間隔後啟動,便沒有了初次運行時多線程同時進入的情況。
timerClose = new System.Threading.Timer(new TimerCallback(timerCall), null , 20, 20);//多次進入
timerClose = new System.Threading.Timer(new TimerCallback(timerCall), null , 1000, 20);//正常進入

測試用的代碼:using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Timers;
using System.Threading;

namespace WindowsApplication3
{
    static class Program
    {
        /**//// <summary>
        /// 應用程式的主進入點。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //System.Threading.Timer thrTimer = new System.Threading.Timer();

            
            

            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

            timer.AutoReset = true;
            timer.Interval =20;

            System.Threading.Timer timerClose;
            //解決初次進入timer調用函數時多線程同時訪問函數的問題
            //Threading和Timers的timer都存在此問題分析後初步判斷是初次運行前的間隔時間的問題
            //Timers的無法設定初次啟動前間隔所以設定20ms間隔時第一次進入前的間隔也是20ms
            //Threading的可以設定初次啟動前的間隔,設定較大間隔後啟動,便沒有了初次運行時多線程同時進入的情況。
            //timerClose = new System.Threading.Timer(new TimerCallback(timerCall), null , 1000, 20);
            //timerClose = new System.Threading.Timer(new TimerCallback(timerCall), null , 20, 20);
            //timer.Start();


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        public static  void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Console.Out.WriteLine("system   :"+DateTime.Now + " " + DateTime.Now.Millisecond + " "+DateTime .Now.TimeOfDay.TotalMilliseconds );

        }
        //object oo = new object();
        //static int inTimer = 0; 
        public static void timerCall(object obj)
        {

            //timerClose.Dispose();
            //lock (this)
            //if (Interlocked.Exchange(ref inTimer, 1) == 0) 
            {
                //Console.Out.WriteLine(Environment.TickCount);
                Console.Out.WriteLine("Threading:" + DateTime.Now + " " + DateTime.Now.Millisecond + " " + DateTime.Now.TimeOfDay.TotalMilliseconds);
                //this.Close();
                //Interlocked.Exchange(ref inTimer, 0); 
            }

        }




    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.