. Net session application Implementation Task Plan code

Source: Internet
Author: User

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>

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.