欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.Net(C#)自动执行计划任务的程序实例分析

程序员文章站 2023-02-28 14:06:01
在业务复杂的应用程序中,有时候会要求一个或者多个任务在一定的时间或者一定的时间间隔内计划进行,比如定时备份或同步,定时发送电子邮件等,我们称之为计划任务。实现计划任务的方法也有很多,可以采用sqla...
在业务复杂的应用程序中,有时候会要求一个或者多个任务在一定的时间或者一定的时间间隔内计划进行,比如定时备份或同步,定时发送电子邮件等,我们称之为计划任务。实现计划任务的方法也有很多,可以采用sqlagent执行存储过程来实现,也可以采用windows任务调度程序来实现,也可以使用windows服务来完成我们的计划任务,这些方法都是很好的解决方案。但是,对于web应用程序来说,这些方法实现起来并不是很简单的,主机服务提供商或者不能直接提供这样的服务,或者需要你支付许多额外的费用。 本文就介绍一个直接在web应用程序中使用的简单的方法,这个方法不需要任何额外的配置即可轻松实现。 

  由于asp.net站点是作为web应用程序运行的,它并不受线程的限制,因此我们可以非常方便地在application_start和application_end事件中建立和销毁一个计划任务。下面就简单介绍一下在web站点实现计划任务的方法。我们的例子是定时往文件里添加信息,作为例子,这里把当前的时间定时地写入文件中。 

  一个计划任务的工作单元称之为一个任务(job),下面的代码描述了对所有任务都可以被调度引擎计划执行的一个通用的接口,这里的每个任务实现了execute方法,供调度引擎进行调用: 

1 public interface ischedulerjob

2     {

3         void execute();

4     }

 

 

  如前所述,我们的例子是实现往文件写如字符日期,下面就是实现这一任务的方法: 

复制代码

 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 } 

复制代码

  接下来,我们建立一个配置对象,告诉调度引擎执行什么任务和执行的时间间隔。 

复制代码

 1 public class schedulerconfiguration 

 2 { 

 3 //时间间隔 

 4 private int sleepinterval; 

 5 //任务列表 

 6 private arraylist jobs = new arraylist(); 

 7 

 8 public int sleepinterval { get { return sleepinterval; } } 

 9 public arraylist jobs { get { return jobs; } } 

10 

11 //调度配置类的构造函数 

12 public schedulerconfiguration(int newsleepinterval) 

13 { 

14 sleepinterval = newsleepinterval; 

15 } 

16 } 

17 

18 下面就是调度引擎,定时执行配置对象的任务 

19 

20 public class scheduler 

21 { 

22 private schedulerconfiguration configuration = null; 

23 

24 public scheduler(schedulerconfiguration config) 

25 { 

26 configuration = config; 

27 } 

28 

29 public void start() 

30 { 

31 while(true) 

32 { 

33 //执行每一个任务 

34 foreach(ischedulerjob job in configuration.jobs) 

35 { 

36 threadstart mythreaddelegate = new threadstart(job.execute); 

37 thread mythread = new thread(mythreaddelegate); 

38 mythread.start(); 

39 thread.sleep(configuration.sleepinterval); 

40 } 

41 } 

42 } 

43 } 

复制代码

 

 

  所有的准备工作已经完成,下面就是激活引擎的工作了。为了让我们的任务计划执行,我们在global.asax.cs文件里的applicatio_start和application_end里进行建立和销毁工作,首先建立一个调度进程运行的线程,我们这里的运行间隔时间为3秒钟。 

复制代码

 1 public system.threading.thread schedulerthread = null; 

 2 protected void application_start(object sender, eventargs e) 

 3 { 

 4 schedulerconfiguration config = new schedulerconfiguration(1000*3); 

 5 config.jobs.add(new samplejob()); 

 6 scheduler scheduler = new scheduler(config); 

 7 system.threading.threadstart mythreadstart = new system.threading.threadstart(scheduler.start); 

 8 system.threading.thread schedulerthread = new system.threading.thread(mythreadstart); 

 9 schedulerthread.start(); 

10 } 

复制代码

 

 

  最后还需要在程序退出时进行销毁: 

复制代码

1 protected void application_end(object sender, eventargs e) 

2 { 

3 if (null != schedulerthread) 

4 { 

5 schedulerthread.abort(); 

6 } 

7 } 

复制代码

 

 

  好了,在vs.net里建立一个c#的web应用程序工程,建立taskscheduler.cs类,并修改相应的global.asax.cs文件。为了能看到效果,我们再建立一个表单webform1.x,定时刷新来检查我们所记录的数据: 

复制代码

 1 <%@ page language="" codebehind="webform1.aspx.cs" autoeventwireup="false" 

 2 inherits="cstest.webform1" %> 

 3 <!doctype html public "-//w3c//dtd html 4.0 transitional//en" > 

 4 <html> 

 5 <head> 

 6 <title>在web应用程序中执行计划任务的例子</title> 

 7 <meta http-equiv="refresh" content="10"> 

 8 <meta name="generator" content="microsoft visual studio 7.0"> 

 9 <meta name="code_language" content="c#"> 

10 <meta name="vs_defaultclientscript" content="javascript"> 

11 <meta name="vs_targetschema" content="https://schemas.microsoft.com/intellisense/ie5"> 

12 </head> 

13 <body ms_positioning="gridlayout"> 

14 <form id="form1" method="post" runat="server"> 

15 <iframe style="width:100%;height:100%" src="schedulerjob/schedulerjob.txt"></iframe> 

16 </form> 

17 </body> 

18 </html> 

复制代码

  对工程进行编译并运行,就可以看到结果了,结果如下: 

 

计划任务测试开始: 

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