asp.net web 定時執行任務

來源:互聯網
上載者:User

 web網站裡面,需要每隔1分鐘,執行一個任務,並且一直保持這個定時執行狀態,可以用如下一個方法:

   1,Global.asax裡面的 Application_Start ,發生在第一次請求網站的時候,網站關閉(iis關閉網站或者刪除網站)

    在寫這個Application_Start  裡面的內容之前,先寫個定時器:

Time_Task

public  class Time_Task    {        public event System.Timers.ElapsedEventHandler ExecuteTask;        private static readonly Time_Task _task = null;        private System.Timers.Timer _timer = null;        //定義時間        private int _interval = 1000;        public int Interval        {            set        {            _interval = value;        }            get            {                return _interval;            }        }        static Time_Task()        {            _task = new Time_Task();        }        public static Time_Task Instance()        {            return _task;        }        //開始        public void Start()        {            if (_timer == null)            {                _timer = new System.Timers.Timer(_interval);                _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);                _timer.Enabled = true;                _timer.Start();            }        }        protected void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)        {            if (null != ExecuteTask)            {                ExecuteTask(sender, e);            }        }        //停止        public void Stop()        {            if (_timer != null)            {                _timer.Stop();                _timer.Dispose();                _timer = null;            }        }    }

2,然後,再寫Global.asax

Global

 public class Global : System.Web.HttpApplication    {        protected void Application_Start(object sender, EventArgs e)        {            // 在應用程式啟動時啟動並執行代碼              Time_Task.Instance().ExecuteTask += new System.Timers.ElapsedEventHandler(Global_ExecuteTask);            Time_Task.Instance().Interval = 1000*10;//表示間隔            Time_Task.Instance().Start();        }                void Global_ExecuteTask(object sender, System.Timers.ElapsedEventArgs e)        {            //在這裡編寫需要定時執行的邏輯代碼        }        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)        {            // 在會話結束時啟動並執行代碼               // 注意: 只有在 Web.config 檔案中的 sessionstate 模式設定為               // InProc 時,才會引發 Session_End 事件。如果會話模式設定為 StateServer               // 或 SQLServer,則不會引發該事件           }        protected void Application_End(object sender, EventArgs e)        {            //  在應用程式關閉時啟動並執行代碼          }    }

然後,只要第一次訪問網站,就會每隔 10秒(自己定義) 執行定義的任務,可以在要執行的任務裡面設定斷點,然後調試...

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.