We have used the asp.net session application feature to implement a task plan. If you need it, please refer to it.
The Code is as follows: |
Copy code |
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. Elaps tutorial ed + = new System. Timers. ElapsedEventHandler (TaskAction. SetContent ); MyTimer. Enabled = true; MyTimer. AutoReset = true; }
|
Application_Start is triggered only once.
The Code is as follows: |
Copy code |
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. You can point to any of your asp tutorial page x or even non-existent pages to stimulate Application_Start // Use your own URL String URLs = http://www.bKjia. c0m; 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
The Code is as follows: |
Copy code |
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 "); } } Output the execution result [Default. aspx] (the page written only for viewing the result) Add <Div> <% = TaskAction. Content %> </Div>
|