[ASP. NET advanced] scheduled task execution, asp.net advanced execution

Source: Internet
Author: User

[ASP. NET advanced] scheduled task execution, asp.net advanced execution

Principle: Use Global applications such as Global. asax and System. Timers. Timer to regularly process tasks.

 

Example:

Its Global. asax class code is as follows:

Using System; using System. collections. generic; using System. IO; using System. linq; using System. net; using System. threading; using System. timers; using System. web; using System. web. security; using System. web. sessionState; namespace TimingTask {/*** principle: Global. asax can be an application or session event handler in asp.net. * Application_Start (Application Start event) and Application_End (Application end event) are used ). * When the application starts, start a timer to regularly execute the YourTask () method. * The logic code to be called can be written in this method, which can be a single thread or multiple threads. * When the application ends, for example, the IIS application pool is recycled, so that asp.net can access the current web address. * Access An aspx page to reactivate the application. */Public class Global: System. web. httpApplication {// code that runs when the application starts protected void Application_Start (object sender, EventArgs e) {// timer System. timers. timer myTimer = new System. timers. timer (2000); myTimer. elapsed + = new ElapsedEventHandler (myTimer_Elapsed); myTimer. enabled = true; myTimer. autoReset = true;} private void myTimer_Elapsed (object source, ElapsedEventArgs e) {try {RunTheTask ();} Catch (Exception ex) {WebForm1.html = ex. message ;}} private void RunTheTask () {// here write the task you want to execute WebForm1.html = DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") + ": AutoTask is Working! ";}// Code run when the new session starts protected void Session_Start (object sender, EventArgs e) {} protected void Application_BeginRequest (object sender, EventArgs e) {} protected void Application_AuthenticateRequest (object sender, EventArgs e) {} // code run when an unprocessed error occurs protected void Application_Error (object sender, EventArgs e) {} // the code that runs when the session ends. Protected void Session_End (object sender, EventArgs e) {// Note: The Session_End event is triggered only when the sessionstate mode in the Web. config file is set to // InProc. If the session mode is set to StateServer // or SQLServer, this event will not be thrown} // the code that runs when the application is closed protected void Application_End (object sender, EventArgs e) {WebForm1.html = DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") + ": Application End! "; // The following code is the key to solve the problem of automatic collection of IIS application pool. Thread. sleep (1000); // set your web address here, which can point to any of your aspx pages or even pages that do not exist, in order to activate Application_Start string url = "WebForm1.aspx "; httpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest. create (url); HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest. getResponse (); Stream receiveStream = myHttpWebResponse. getResponseStream (); // get the write-back byte stream }}}

 

Then, use the WebForm page to output the scheduled effect:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace TimingTask{    public partial class WebForm1 : System.Web.UI.Page    {        public static String html = "";        protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                GetDataBind();            }        }        private void GetDataBind()        {            for (int i = 0; i <10; i++)            {                System.Threading.Thread.Sleep(1000);                Response.Write(html+"<br />");            }        }    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.