一个定时完成任务的类(使用线程)
1 using System;
2 using System.Threading;
3 using System.Collections;
4 using System.Data;
5 using System.Configuration;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Web.UI.HtmlControls;
12 namespace Xr
13 {
14
15
16 /// <summary>
17 /// IScheduler 的摘要说明。
18 /// </summary>
19 public interface ISchedulerJob
20 {
21 void Execute();
22 }
23
24
25 #region SchedulerConfiguration,主要实现调度的配置,保存调度任务和设置调度的间隔时间
26 public class SchedulerConfiguration
27 {
28 private int sleepInterval;
29 private ArrayList jobs = new ArrayList();
30
31 public int SleepInterval { get { return sleepInterval; } }
32 public ArrayList Jobs { get { return jobs; } }
33
34 public SchedulerConfiguration(int newSleepInterval)
35 {
36 sleepInterval = newSleepInterval;
37 }
38
39 }
40 #endregion
41
42 #region Scheduler,调度的核心程序,启动必须要得工作线程
43
44 public class Scheduler
45 {
46 private SchedulerConfiguration configuration = null;
47
48 public Scheduler(SchedulerConfiguration config)
49 {
50 configuration = config;
51 }
52
53 public void Start()
54 {
55 while (true)
56 {
57 try
58 {
59 //for each job, call the execute method
60 foreach (ISchedulerJob job in configuration.Jobs)
61 {
62 job.Execute();
63 }
64 }
65 catch { }
66 finally
67 {
68 Thread.Sleep(configuration.SleepInterval);
69 }
70 }
71 }
72 }
73
74
75 #endregion
76
77 #region 调度的代理模块,目前比较简单,就提供了start和stop的方法,另外就是查看是否正在运行
78 public class SchedulerAgent
79 {
80 private static System.Threading.Thread schedulerThread = null;
81 public static void StartAgent()
82 {
83 SchedulerConfiguration config =
84 new SchedulerConfiguration(3600 * 1000);
85 //3600秒扫描一次
86 config.Jobs.Add(new AlertJob());
87 Scheduler scheduler = new Scheduler(config);
88 System.Threading.ThreadStart myThreadStart =
89 new System.Threading.ThreadStart(scheduler.Start);
90 schedulerThread = new System.Threading.Thread(myThreadStart);
91 schedulerThread.Start();
92 }
93 public static void Stop()
94 {
95 if (null != schedulerThread)
96 {
97 schedulerThread.Abort();
98 }
99 }
100 public static bool Status
101 {
102 get
103 {
104 return (schedulerThread == null) ? false : schedulerThread.IsAlive;
105 }
106 }
107 }
108 #endregion
109
110 #region 调度工作的示范类,实现ISchedulerJob接口就可以
111 public class AlertJob : ISchedulerJob
112 {
113
114
115 public AlertJob()
116 {
117
118
119 }
120
121 /// <summary>
122 /// 定时完成具体的某项事务
123 /// </summary>
124 public void Execute()
125 {
126 // TODO: 添加 FileWriteJob.Execute 实现
127
128
129
130
131
132 }
133 }
134 #endregion
135 }
2 using System.Threading;
3 using System.Collections;
4 using System.Data;
5 using System.Configuration;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Web.UI.HtmlControls;
12 namespace Xr
13 {
14
15
16 /// <summary>
17 /// IScheduler 的摘要说明。
18 /// </summary>
19 public interface ISchedulerJob
20 {
21 void Execute();
22 }
23
24
25 #region SchedulerConfiguration,主要实现调度的配置,保存调度任务和设置调度的间隔时间
26 public class SchedulerConfiguration
27 {
28 private int sleepInterval;
29 private ArrayList jobs = new ArrayList();
30
31 public int SleepInterval { get { return sleepInterval; } }
32 public ArrayList Jobs { get { return jobs; } }
33
34 public SchedulerConfiguration(int newSleepInterval)
35 {
36 sleepInterval = newSleepInterval;
37 }
38
39 }
40 #endregion
41
42 #region Scheduler,调度的核心程序,启动必须要得工作线程
43
44 public class Scheduler
45 {
46 private SchedulerConfiguration configuration = null;
47
48 public Scheduler(SchedulerConfiguration config)
49 {
50 configuration = config;
51 }
52
53 public void Start()
54 {
55 while (true)
56 {
57 try
58 {
59 //for each job, call the execute method
60 foreach (ISchedulerJob job in configuration.Jobs)
61 {
62 job.Execute();
63 }
64 }
65 catch { }
66 finally
67 {
68 Thread.Sleep(configuration.SleepInterval);
69 }
70 }
71 }
72 }
73
74
75 #endregion
76
77 #region 调度的代理模块,目前比较简单,就提供了start和stop的方法,另外就是查看是否正在运行
78 public class SchedulerAgent
79 {
80 private static System.Threading.Thread schedulerThread = null;
81 public static void StartAgent()
82 {
83 SchedulerConfiguration config =
84 new SchedulerConfiguration(3600 * 1000);
85 //3600秒扫描一次
86 config.Jobs.Add(new AlertJob());
87 Scheduler scheduler = new Scheduler(config);
88 System.Threading.ThreadStart myThreadStart =
89 new System.Threading.ThreadStart(scheduler.Start);
90 schedulerThread = new System.Threading.Thread(myThreadStart);
91 schedulerThread.Start();
92 }
93 public static void Stop()
94 {
95 if (null != schedulerThread)
96 {
97 schedulerThread.Abort();
98 }
99 }
100 public static bool Status
101 {
102 get
103 {
104 return (schedulerThread == null) ? false : schedulerThread.IsAlive;
105 }
106 }
107 }
108 #endregion
109
110 #region 调度工作的示范类,实现ISchedulerJob接口就可以
111 public class AlertJob : ISchedulerJob
112 {
113
114
115 public AlertJob()
116 {
117
118
119 }
120
121 /// <summary>
122 /// 定时完成具体的某项事务
123 /// </summary>
124 public void Execute()
125 {
126 // TODO: 添加 FileWriteJob.Execute 实现
127
128
129
130
131
132 }
133 }
134 #endregion
135 }
可以在Global.asax中直接调用SchedulerAgent的静态方法,当然可以提供一些构造的参数进行初值化。
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
Xr.SchedulerAgent.StartAgent();
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
Xr.SchedulerAgent.Stop();
}