Web Background running task (zz)

Source: Internet
Author: User
In complex business applications Program In, sometimes one or more tasks are required to be scheduled at a certain time or within a certain interval, such as regular backup or Database Synchronization, and regular email sending, regular processing of user status information and regular synchronization of abnormal bills in the payment system are called scheduled tasks. There are also many ways to schedule tasks. You can use SQLAgent to execute stored procedures, use Windows Task Scheduling programs, or use Windows Services to complete our scheduled tasks, these methods are good solutions. However, all of the preceding operations can only be performed with server permissions. For Web applications used by VM customers, these methods are not very easy to implement, host service providers cannot directly provide such services, or you need to pay a lot of extra fees. I have read some Article It is found that there is a common drawback: When IIS runs for a certain period of time, the scheduled task stops. The reason is that the application pool of IIS is recycled regularly, causing the scheduled task to stop. This method in this article can solve the application pool recycle problem.
See Code :
Global. asax. CS

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. sessionstate;
Using system. Timers;
Using system. net;
Using system. IO;
Using system. text;
Using system. Threading;
// Mobile theme http://www.qumiao.com
// Email: haolinks # 163.com
// Author: Walking sweet
Namespace qumiao.com
{
Public class Global: system. Web. httpapplication
{

Protected void application_start (Object sender, eventargs E)
{
// Define the timer
System. Timers. Timer mytimer = new system. Timers. Timer (5000 );
Mytimer. elapsed + = new elapsedeventhandler (mytimer_elapsed );
Mytimer. Enabled = true;
Mytimer. autoreset = true;
}

Void mytimer_elapsed (Object source, elapsedeventargs E)
{
Try
{
Log. savenote (datetime. Now. tostring ("yyyy-mm-dd hh: mm: SS") + ": autotask is working! ");
Yourtask ();
}
Catch (exception ee)
{
Log. saveexception (EE );
}
}

Void yourtask ()
{
// Write the task you want to execute here
}

Protected void application_end (Object sender, eventargs E)
{
Log. savenote (datetime. Now. tostring ("yyyy-mm-dd hh: mm: SS") + ": Application end! ");

// The following code is the key to solve the problem of automatic collection of IIS application pool
Thread. Sleep (1000 );
// Set your web address here, which can point to any of your Aspx pages or even non-existent pages to stimulate application_start
// String url = "http://www.qumiao.com"; mobile phone subject
String url = "http: // localhost: 82/111. aspx ";
Httpwebrequest myhttpwebrequest = (httpwebrequest) webrequest. Create (URL );
Httpwebresponse myhttpwebresponse = (httpwebresponse) myhttpwebrequest. getresponse ();
Stream receivestream = myhttpwebresponse. getresponsestream (); // get the write-back byte stream

}
}
}

Principle: Global. asax can be an application or session event handler in Asp.net. We use application_start (Application Start event) and application_end (Application end event ). When the application starts, start a timer to regularly execute the yourtask () method. In this method, you can write the logic code to be called, which can be single-thread and multi-thread. When the application ends, for example, the application pool of IIS is recycled, Asp.net is allowed to access the current web address. During local testing, write the local address, such as http: // localhost: 82/111. aspx, to the formal production environment, can be changed to your web address, such as mobile phone theme http://www.qumiao.com etc. Access An ASPX page to reactivate the application. The log class is a log recording class. The following is the log information generated during the test:
========================================================== ======================================
2008-10-30 17: 46: 10: autotask is working!
2008-10-30 17: 46: 15: autotask is working!
2008-10-30 17: 46: 20: autotask is working!
2008-10-30 17: 46: 23: Application end!
2008-10-30 17: 46: 29: autotask is working!
2008-10-30 17: 46: 34: autotask is working!
The log shows that after the application pool of IIS is manually reclaimed, the scheduled task is still being executed, which indicates that our goal has been achieved.
If you comment out the code in application_end, you will find that the scheduled task stops working after the application end, as shown below:
========================================================== ======================================
2008-10-30 18: 01: 34: autotask is working!
2008-10-30 18: 01: 39: autotask is working!
2008-10-30 18: 01: 44: autotask is working!
2008-10-30 18: 01: 46: Application end!

Limitations: it can solve the problem of automatic or manual collection of application pools, but it cannot solve the problem of IIS restart or web server restart. Of course, this situation may not occur many times, if someone visits your website, the scheduled task is automatically activated.

Code download: http://www.qumiao.com/down/autotask.rar

I would like to share with you a little bit of experience. You are welcome to make improvements and repost them. As an encouragement to the author, I am very grateful to you for retaining the copyright information!

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.