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

Source: Internet
Author: User

Follow these steps:
1. Make the following changes in the Global. asax file:
Copy codeThe Code is as follows:
Void Application_Start (object sender, EventArgs e)
{
// Code that runs when the application starts
// Define the timer
// 1000 indicates the meaning of 1 second
System. Timers. Timer myTimer = new System. Timers. Timer (1000 );
// TaskAction. SetContent indicates the method to be called
MyTimer. Elapsed + = new System. Timers. ElapsedEventHandler (TaskAction. SetContent );
MyTimer. Enabled = true;
MyTimer. AutoReset = true;
}

Application_Start is triggered only once.
Copy codeThe Code is as follows:
Void Session_End (object sender, EventArgs e)
{
// The following code is the key to solve the problem of automatic collection of IIS application pool
System. Threading. Thread. Sleep (1000 );
// Trigger the event and write the prompt information
TaskAction. SetContent ();
// Set your web address here, which can point to any of your aspx pages or even non-existent pages to stimulate 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 the write-back byte Stream
// The code that runs when the session ends.
// 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, the event is not triggered.
}

Session_End mainly solves the issue of automatic collection of IIS application pool.
2. Add a scheduled task class TaskAction
Copy codeThe Code is as follows:
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>
/// 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
Copy codeThe Code is as follows:
<Div>
<% = TaskAction. Content %>
</Div>

4. result output

You are welcome to discuss
Download sample source code

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.