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