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