.net利用Timer和Global.asax實現定時執行程式C#

來源:互聯網
上載者:User

public class TestTimer
    {
     
        //TimerCallback 委託,GlobalTimer_ToDo表示要執行的方法  
        public static Timer GlobalTimer = new Timer(new TimerCallback(GlobalTimer_ToDo), null, Timeout.Infinite, Timeout.Infinite);

        /*也可以直接定時 
        GlobalTimer.Interval = 10; 
                GlobalTimer.Enabled = true; 
                GlobalTimerAutoReset = true;*/

        static void GlobalTimer_ToDo(object obj)
        {
            if (DateTime.Now.Hour == 0)
            {//判斷多少時間執行一次

            }
        }

        public static void Start(long a, long b)
        {
            //Timer.Change(Int32, Int32)方法用來更改計時器的啟動時間和方法調用之間的間隔,用 32 位有符號整數度量時間間隔  
            GlobalTimer.Change(a, b);
        }

        public static void Stop()
        {
            //Timeout.Infinite是用於指定無限長等待時間的常數  
            GlobalTimer.Change(Timeout.Infinite, Timeout.Infinite);
        }
        //停止定時器調用TestTimer.Stop();釋放定時器調用TestTimer.GlobalTimer.Dispose();
    }
 public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            //參數0表示立即執行定時器,60000單位毫秒,就是一分鐘執行一次  一小時 (3600000)
            TestTimer.Start(0, 10000);
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }

天翼 三星W799 雙模手機CDMA雙攝FM雙屏+JAVA炒股QQ地圖2G記憶體
980.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.