Asp. Net (C #) program instance analysis for Automatic Execution of scheduled tasks

Source: Internet
Author: User

In applications with complex services, sometimes one or more tasks are required to be scheduled at a certain time or interval, such as regular backup or database synchronization, scheduled sending of emails is 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 schedule tasks, these methods are good solutions. However, for Web applications, the implementation of these methods is not very simple, host service providers or cannot directly provide such services, or you need to pay a lot of extra fees. This article introduces a simple method that is directly used in Web applications. This method can be easily implemented without any additional configuration.
Because the ASP. NET Site runs as a Web application and is not restricted by threads, we can easily create and destroy a scheduler task in Application_Start and Application_End events. The following describes how to plan tasks on a Web site. In our example, information is regularly added to the file. As an example, the current time is regularly written to the file.
The unit of work of a scheduled task is called a Job. The following code describes a common interface that can be executed by the scheduling engine for all tasks, each task implements the Execute method for the scheduling engine to call:

                 }

 

As mentioned above, our example is to write data to a file as a character date. The following describes how to implement this task:

         p =     FILE_NAME = p+    c = System.DateTime.Now.ToString(   flag =    (!  flag =  StreamWriter sr =    StreamWriter x =  StreamWriter(FILE_NAME, (flag) x.Write( x.Write(+   } 

Next, we will create a configuration object to tell the scheduling engine what task to execute and the interval between the execution.

         ArrayList jobs =     SleepInterval {  {   ArrayList Jobs {  {     SchedulerConfiguration(  sleepInterval =          SchedulerConfiguration configuration =     configuration =      (   (ISchedulerJob job   ThreadStart myThreadDelegate =  Thread myThread =       } 

 

All preparations have been completed. The following describes how to activate the engine. In order to make our task plan run, we. asax. in the cs file Applicatio_Start and Application_End, create and destroy a thread where the scheduling process runs. The running interval here is 3 seconds.

  System.Threading.Thread schedulerThread =     SchedulerConfiguration config =  SchedulerConfiguration(* config.Jobs.Add( Scheduler scheduler =  System.Threading.ThreadStart myThreadStart =  System.Threading.Thread schedulerThread =   } 

 

Finally, destroy the program upon exit:

     ( !=    } 

 

All right, create a C # Web application project in VS. NET, create TaskScheduler. cs class, and modify the corresponding Global. asax. cs file. In order to see the effect, we create another form WebForm1.aspx and refresh it regularly to check the data we recorded:

<% @ Page language = Codebehind = AutoEventWireup = Inherits = %> <! Doctype html public> <HTML> <HEAD> <title> example of executing a scheduled task in a Web application </title> <meta http-equiv = content => <meta name = content => <meta name = content => </HEAD> <body MS_POSITIONING => <form id = method = runat ==>< iframe style = src => </iframe> </form> </body> </HTML>

Compile and run the project. The result is as follows:

Scheduled task test starts:
2003-13-10 11:08:15
2003-13-10 11:08:18
2003-13-10 11:08:21
2003-13-10 11:08:24
2003-13-10 11:08:27
2003-13-10 11:08:30

It should be noted that the preceding is a simple example of executing a scheduled task in a Web application. For multiple tasks, they need to work in different threads, the plan is also very simple, and the actual site needs to be blocked, when the machine is in the case. In addition, there is no such work as error handling here. I believe everyone will write more perfect code.

Download source code: http://files.cnblogs.com/zhaoxuntao/AutoRun.zip

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.