"ASP." Scheduled Task execution

Source: Internet
Author: User

Principle: Use the Global Application class Global.asax and System.Timers.Timer classes to process tasks on a timed basis.

Example:

The Global.asax class code is as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Threading;usingsystem.timers;usingsystem.web;usingSystem.Web.Security;usingSystem.Web.SessionState;namespacetimingtask{/** * Principle: Global.asax can be an application or Session event handler in ASP. * We use Application_Start (Application start Event) and Application_End (Application End event).     * When the application starts, start a timer to execute the task Yourtask () method, * This method can write the logic code that needs to be called, can be single thread and multithreading.     * When the application ends, such as IIS's application pool recycling, let ASP. NET to access the current web address.      * There is a need to access an ASPX page so that you can reactivate the application. */     Public classGlobal:System.Web.HttpApplication {//code to run when the application starts        protected voidApplication_Start (Objectsender, EventArgs e) {            //TimerSystem.Timers.Timer MyTimer =NewSystem.Timers.Timer ( -); Mytimer.elapsed+=NewElapsedeventhandler (mytimer_elapsed); Mytimer.enabled=true; Mytimer.autoreset=true; }        Private voidMytimer_elapsed (Objectsource, Elapsedeventargs E) {            Try{runthetask (); }            Catch(Exception ex) {webform1.html=Ex.            Message; }        }        Private voidRunthetask () {//write the tasks you need to perform here.webform1.html = DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss") +": Autotask is working!"; }        //code to run when a new session is started        protected voidSession_Start (Objectsender, EventArgs e) {        }        protected voidApplication_BeginRequest (Objectsender, EventArgs e) {        }        protected voidApplication_AuthenticateRequest (Objectsender, EventArgs e) {        }        //code to run when an unhandled error occurs        protected voidApplication_Error (Objectsender, EventArgs e) {        }        //The code that runs at the end of the session.         protected voidSession_End (Objectsender, EventArgs e) {            //Note: Only the sessionstate mode in the Web. config file is set to//InProc, the Session_End event is not raised. If the session mode is set to StateServer//or SQL Server, the event is not raised        }        //code to run when the application shuts down        protected voidApplication_End (Objectsender, EventArgs e) {webform1.html= DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss") +": Application end!"; //The following code is critical to resolve an issue with automatic collection of IIS application poolsThread.Sleep ( +); //Set your web address here and you can point to any of your ASPX pages or even nonexistent pages to inspire Application_Start            stringURL ="WebForm1.aspx"; HttpWebRequest myHttpWebRequest=(HttpWebRequest) webrequest.create (URL); HttpWebResponse Myhttpwebresponse=(HttpWebResponse) myhttpwebrequest.getresponse (); Stream Receivestream= Myhttpwebresponse.getresponsestream ();//get write-back byte stream        }    }}

Then use the WebForm page to output the timing effect:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacetimingtask{ Public Partial classWebForm1:System.Web.UI.Page { Public StaticString HTML =""; protected voidPage_Load (Objectsender, EventArgs e) {            if(!IsPostBack)            {Getdatabind (); }        }        Private voidGetdatabind () { for(inti =0; I <Ten; i++) {System.Threading.Thread.Sleep ( +); Response.Write (HTML+"<br/>"); }        }    }}

"ASP." Scheduled Task execution

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.