web scheduled task execution

Source: Internet
Author: User

In the Web site, you need to execute a task every one minute and keep the scheduled execution status. You can use the following method:

1. application_start in global. asax occurs when the website is requested for the first time (IIS closes or deletes the website)

Before writing the content in application_start, write a Timer:


Public class time_task {public event system. timers. elapsedeventhandler executetask; Private Static readonly time_task _ task = NULL; private system. timers. timer _ timer = NULL; // defines the time private int _ interval = 1000; Public int interval {set {_ interval = value;} get {return _ interval ;}} static time_task () {_ task = new time_task ();} public static time_task instance () {return _ task;} // start publi C void start () {If (_ timer = NULL) {_ timer = new system. timers. timer (_ interval); _ timer. elapsed + = new system. timers. elapsedeventhandler (_ timer_elapsed); _ timer. enabled = true; _ timer. start () ;}} protected void _ timer_elapsed (Object sender, system. timers. elapsedeventargs e) {If (null! = Executetask) {executetask (sender, e) ;}// stop public void stop () {If (_ timer! = NULL) {_ timer. Stop (); _ timer. Dispose (); _ timer = NULL ;}}}

2. Then write global. asax.


Public class Global: system. web. httpapplication {protected void application_start (Object sender, eventargs e) {// code time_task.instance () that runs when the application starts (). executetask + = new system. timers. elapsedeventhandler (global_executetask); time_task.instance (). interval = 1000*10; // indicates the interval time_task.instance (). start ();} void global_executetask (Object sender, system. timers. elapsedeventargs e) {// write the logic code to be periodically executed here} Protected void session_start (Object sender, eventargs e) {// code to run when a new session starts} protected void application_beginrequest (Object sender, eventargs E) {} protected void application_authenticaterequest (Object sender, eventargs e) {} protected void application_error (Object sender, eventargs E) {// code to run when an unprocessed error occurs} protected void session_end (Object sender, eventargs e) {// code to run when the session ends // note: only in the web. c The session_end event is triggered only when the sessionstate mode in the onfig file is set to // inproc. If the session mode is set to StateServer // or sqlserver, this event is not thrown} protected void application_end (Object sender, eventargs e) {// code that runs when the application is closed }}

Then, as long as you access the website for the first time, you will execute the defined task every 10 seconds (defined by yourself). You can set a breakpoint in the task to be executed, and then DEBUG...



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: 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.