ASP.net plan task implementation method (without add-in,. NET internal mechanism Implementation) _ Practical Tips

Source: Internet
Author: User
The following steps are explained:
1. The following modifications are made in the Global.asax document
Copy Code code as follows:

void Application_Start (object sender, EventArgs e)
{
Code that runs when the application starts
Defining timers
1000 means 1 seconds.
System.Timers.Timer MyTimer = new System.Timers.Timer (1000);
Taskaction.setcontent represents the method to invoke
mytimer.elapsed + = new System.Timers.ElapsedEventHandler (taskaction.setcontent);
Mytimer.enabled = true;
Mytimer.autoreset = true;
}

Application_Start is triggered only once after a visit.
Copy Code code as follows:

void Session_End (object sender, EventArgs e)
{
The following code is critical to solve the problem of automatic recycling of IIS application pools
System.Threading.Thread.Sleep (1000);
Triggers the event, writes the hint information
Taskaction.setcontent ();
Set up your web address here, you can easily point to any of your ASPX pages or even non-existent pages, in order to inspire Application_Start
Use your own URL
String url = "Http://henw.cnblog.com";
System.Net.HttpWebRequest myhttpwebrequest = (System.Net.HttpWebRequest) System.Net.WebRequest.Create (URL);
System.Net.HttpWebResponse myhttpwebresponse = (System.Net.HttpWebResponse) myhttpwebrequest.getresponse ();
System.IO.Stream Receivestream = Myhttpwebresponse.getresponsestream ();//get writeback byte stream
Code that runs at the end of the session.
Note: Session_End events are raised only if the sessionstate mode in the Web.config file is set to InProc.
If the session mode is set to StateServer
or SQL Server, the event is not raised.
}

The approach in Session_End is primarily to solve the problem of automatic collection of IIS application pools.
2. Add Scheduled Task class Taskaction
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Timers;
<summary>
Summary description of Action
</summary>
public static Class Taskaction
{
private static string content = "";
<summary>
Where the output information is stored.
</summary>
public static string Content
{
get {return taskaction.content;}
set {taskaction.content + = "<div>" + value+ "</div>";}
}
<summary>
The method of the timer delegate task call
</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 invoked when pool recycle is applied
</summary>
public static void SetContent ()
{
Content = "End:" + DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss");
}
}

3. Execution results output [default.aspx] (This step is only for viewing the results of the page written)
Add on Default.aspx page
Copy Code code as follows:

<div>
<%=taskaction.content%>
</div>

4. Results output

Welcome to join us in exploring
Sample source code Download

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.