asp.net (C #) Program instance analysis for automatic execution of scheduled tasks

Source: Internet
Author: User
Tags config current time thread time interval

In a business complex application, it is sometimes required that one or more tasks be scheduled for a certain amount of time or time interval, such as a scheduled backup or synchronization of a database, a timed email, etc., which we call a scheduled task. There are a number of ways to implement a scheduled task, either by using SQLAgent to execute stored procedures, by using Windows Task Scheduler, or by using Windows services to complete our scheduled tasks, which are good solutions. However, for Web applications, these methods are not easy to implement, and host service providers either cannot provide such services directly or require you to pay a lot of extra costs. This article describes an easy way to use directly in a Web application, which is easy to implement without any additional configuration.

Because the ASP.net site is run as a Web application, it is not constrained by threads, so we can easily create and destroy a scheduled task in the Application_Start and Application_End events. Here's a quick overview of how to implement a scheduled task on a Web site. Our example is to periodically add information to the file, as an example, where the current time is written to the file at timed intervals.

A working unit of a scheduled task is called a task (job), and the following code describes a generic interface for all tasks to be scheduled by the dispatch engine, where each task implements the Execute method for the dispatch engine to invoke:

1 public Interface Ischedulerjob

2 {

3 void Execute ();

4}

As mentioned earlier, our example is to implement writing to a file as a character date, the following is the way to achieve this task:

1 public class SampleJob : ISchedulerJob
2 {
3 public void Execute()
4 {
5 //文件保存的物理路径,CSTest为虚拟目录名称,F:\Inetpub\wwwroot\CSTest为物理路径
6 string p = @"C:\Users\Jack\Desktop\AutoRun\AutoRun";
7 //我们在虚拟目录的根目录下建立SchedulerJob文件夹,并设置权限为匿名可修改,
8 //SchedulerJob.txt就是我们所写的文件
9 string FILE_NAME = p+ "\\SchedulerJob\\SchedulerJob.txt";
10 //取得当前服务器时间,并转换成字符串
11 string c = System.DateTime.Now.ToString("yyyy-mm-dd hh:MM:ss");
12 //标记是否是新建文件的标量
13 bool flag = false;
14 //如果文件不存在,就新建该文件
15 if (!File.Exists(FILE_NAME))
16 {
17 flag = true;
18 StreamWriter sr = File.CreateText(FILE_NAME);
19 sr.Close();
20 }
21 //向文件写入内容
22 StreamWriter x = new StreamWriter(FILE_NAME,true,System.Text.Encoding.Default);
23 if(flag) x.Write("计划任务测试开始:");
24 x.Write("\r\n"+c);
25 x.Close();
26 }
27 }

Next, we set up a configuration object that tells the dispatch engine what tasks to perform and what time interval to perform.

 public class Schedulerconfiguration {//time interval private int sleepinterval;    
       
Task List Private ArrayList jobs = new ArrayList ();    
public int Sleepinterval {get {return sleepinterval;}}    
       
Public ArrayList Jobs {get {return jobs;}}    
The constructor of the dispatch configuration class is public schedulerconfiguration (int newsleepinterval) {sleepinterval = Newsleepinterval; The following is the dispatch engine, which performs the tasks of the configuration object regularly Scheduler {private schedulerconfiguration confi    
       
Guration = null;    
Public Scheduler (schedulerconfiguration config) {configuration = config; public void Start () {while (true) {//Execute each task foreach (Ischedulerjob job in configuration . Jobs) {ThreadStart mythreaddelegate = new ThreadStart (Job).    
Execute);    
Thread mythread = new Thread (mythreaddelegate);    
Mythread.start (); Thread.Sleep (configuration.    
Sleepinterval); }    
}    
}    
}

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.