ASP. NET scheduled tasks (implemented by. net internal mechanism without using external programs)

Source: Internet
Author: User

In Asp.net, you can only use a timer to check and execute tasks without using other plug-ins.

Follow these steps:

1. Make the following changes in the global. asax file:

 
Void application_start (Object sender, eventargs e) {//ProgramTheCode// Define the timer // 1000 indicates the meaning of 1 second system. timers. timer mytimer = new system. timers. timer (1000); // taskaction. setcontent indicates the method mytimer to be called. elapsed + = new system. timers. elapsedeventhandler (taskaction. setcontent); mytimer. enabled = true; mytimer. autoreset = true ;}

Application_start is triggered only once.

 

Void session_end (Object sender, eventargs e) {// the following code is critical and can solve the problem of automatic collection of IIS application pool system. threading. thread. sleep (1000); // trigger the event and write the prompt message taskaction. setcontent (); // set your web address here, which can point to any of your Aspx pages or even non-existent pages, the purpose is to inspire application_start // use your own urlstring url = "http://henw.cnblog.com"; system. net. httpwebrequest myhttpwebrequest = (system. net. httpwebrequest) system. net. webrequest. create (URL); system. net. httpwebresponse myhtt Pwebresponse = (system. net. httpwebresponse) myhttpwebrequest. getresponse (); system. io. stream receivestream = myhttpwebresponse. getresponsestream (); // get the write-back byte stream // code that runs at the end of the session. // 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 is not triggered .}

Session_end mainly solves the issue of automatic collection of IIS application pool.

 

2. Add a scheduled task class taskaction

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. timers; // <summary> // abstract description of action /// </Summary> Public static class taskaction {Private Static string content = ""; /// <summary> /// the location where the output information is stored. /// </Summary> Public static string content {get {return taskaction. content;} set {taskaction. content + = "<div>" + value + "</div> ";}} /// <summary> /// Method for calling the timer delegate task /// </Summary> /// <Param name = "Source"> </param> /// <Param name = "E"> </param> Public static void setcontent (Object source, elapsedeventargs e) {content = datetime. now. tostring ("yyyy-mm-dd hh: mm: SS ");} /// <summary> /// method called when the application pool is recycled /// </Summary> Public static void setcontent () {content = "end:" + datetime. now. tostring ("yyyy-mm-dd hh: mm: SS ");}}

3. Execution result output [default. aspx] (the page written only for viewing results)
Add

 
<Div> <% = taskaction. Content %> </div>

 

4. result output

You are welcome to discuss

ExampleSource codeDownload

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.