C#通过线程完成定时执行事件
软件中经常有定时执行的操作,比如银行需要定时计算利息,客户合同到期需要定时自动发送短信或者邮件提醒,这些如果通过人工操作是一个很大的任务量,不太切合实际,所以需要考虑让系统自动完成操作,方法很多,比如数据库的定时作业,window组件等都可以实现,在这里我们使用另外一种方式,C#线程来完成:
首先定义一个接口(ISchedulerJob),用来标准化事件的任务
View Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
/// <summary>
///ISchedulerJob 的摘要说明
/// </summary>
public interface ISchedulerJob
{
void Execute();
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
/// <summary>
///ISchedulerJob 的摘要说明
/// </summary>
public interface ISchedulerJob
{
void Execute();
}
然后定义任务类实现接口
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Data.SqlClient;
using JH.BLL;
using JH.Model;
/// <summary>
///SampleJob 的摘要说明
/// </summary>
public class SampleJob : ISchedulerJob
{
public SampleJob()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
#region ISchedulerJob 成员
public void Execute()
{
string today = DateTime.Now.ToString("MM-dd HH:mm:ss");
try
{
//获取所有用户
//如果时间处在下面的四个时间点,将会执行定义的任务
if (today == "03-20 00:20:00" || today == "06-20 00:20:00" || today == "09-20 00:20:00" || today == "12-20 00:20:00")
{
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Data.SqlClient;
using JH.BLL;
using JH.Model;
/// <summary>
///SampleJob 的摘要说明
/// </summary>
public class SampleJob : ISchedulerJob
{
public SampleJob()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
#region ISchedulerJob 成员
public void Execute()
{
string today = DateTime.Now.ToString("MM-dd HH:mm:ss");
try
{
//获取所有用户
//如果时间处在下面的四个时间点,将会执行定义的任务
if (today == "03-20 00:20:00" || today == "06-20 00:20:00" || today == "09-20 00:20:00" || today == "12-20 00:20:00")
{
//任务
}
}
}
}
}
}
}
catch (Exception ee)
{
throw new Exception(ee.Message);
}
}
#endregion
}
}
}
}
}
catch (Exception ee)
{
throw new Exception(ee.Message);
}
}
#endregion
}
如果有多个任务,可以多定义几个
然后在一个类用来将任务整合到一起,并设其他要用到的参数
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
/// <summary>
///SchedulerConfiguration 的摘要说明
/// </summary>
public class SchedulerConfiguration
{
//时间间隔
private int sleepInterval;
//任务列表
private ArrayList jobs = new ArrayList();
public int SleepInterval { get { return sleepInterval; } }
public ArrayList Jobs { get { return jobs; } }
public SchedulerConfiguration(int newSleepInterval)
{
sleepInterval = newSleepInterval;
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
/// <summary>
///SchedulerConfiguration 的摘要说明
/// </summary>
public class SchedulerConfiguration
{
//时间间隔
private int sleepInterval;
//任务列表
private ArrayList jobs = new ArrayList();
public int SleepInterval { get { return sleepInterval; } }
public ArrayList Jobs { get { return jobs; } }
public SchedulerConfiguration(int newSleepInterval)
{
sleepInterval = newSleepInterval;
}
}
再次开始执行任务
View Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Threading;
/// <summary>
///Scheduler 的摘要说明
/// </summary>
public class Scheduler
{
private SchedulerConfiguration configuration = null;
public Scheduler(SchedulerConfiguration config)
{
//
//TODO: 在此处添加构造函数逻辑
//
configuration = config;
}
public void Start()
{
while (true)
{
//执行每一个任务
foreach (ISchedulerJob job in configuration.Jobs)
{
ThreadStart myThreadDelegate = new ThreadStart(job.Execute);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();
Thread.Sleep(configuration.SleepInterval);
}
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Threading;
/// <summary>
///Scheduler 的摘要说明
/// </summary>
public class Scheduler
{
private SchedulerConfiguration configuration = null;
public Scheduler(SchedulerConfiguration config)
{
//
//TODO: 在此处添加构造函数逻辑
//
configuration = config;
}
public void Start()
{
while (true)
{
//执行每一个任务
foreach (ISchedulerJob job in configuration.Jobs)
{
ThreadStart myThreadDelegate = new ThreadStart(job.Execute);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();
Thread.Sleep(configuration.SleepInterval);
}
}
}
}
最后,这些都定义好了,我们需要通过线程不断去触发,可以再服务启动的时候就开始执行,我们添加一个全局应用程序Global.asax
<%@ Application Language="C#" %>
<%@ Import Namespace=" System.Data" %>
<%@ Import Namespace=" JH.BLL" %>
<%@ Import Namespace="JH.Model" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="JH.Common" %>
<script runat="server">
public System.Threading.Thread schedulerThread = null;
void Application_Start(object sender, EventArgs e)
{
DateTime today = DateTime.Now;
SchedulerConfiguration config = new SchedulerConfiguration(1000);
config.Jobs.Add(new SampleJob());
Scheduler scheduler = new Scheduler(config);
System.Threading.ThreadStart myThreadStart = new System.Threading.ThreadStart(scheduler.Start);
System.Threading.Thread schedulerThread = new System.Threading.Thread(myThreadStart);
schedulerThread.Start();
//}
}
void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时,终止线程的进行
if (null != schedulerThread)
{
schedulerThread.Abort();
}
}
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
//在新会话启动时运行的代码
}
void Session_End(object sender, EventArgs e)
{
//在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式
//设置为 StateServer 或 SQLServer,则不会引发该事件。
}
</script>
<%@ Import Namespace=" System.Data" %>
<%@ Import Namespace=" JH.BLL" %>
<%@ Import Namespace="JH.Model" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="JH.Common" %>
<script runat="server">
public System.Threading.Thread schedulerThread = null;
void Application_Start(object sender, EventArgs e)
{
DateTime today = DateTime.Now;
SchedulerConfiguration config = new SchedulerConfiguration(1000);
config.Jobs.Add(new SampleJob());
Scheduler scheduler = new Scheduler(config);
System.Threading.ThreadStart myThreadStart = new System.Threading.ThreadStart(scheduler.Start);
System.Threading.Thread schedulerThread = new System.Threading.Thread(myThreadStart);
schedulerThread.Start();
//}
}
void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时,终止线程的进行
if (null != schedulerThread)
{
schedulerThread.Abort();
}
}
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
//在新会话启动时运行的代码
}
void Session_End(object sender, EventArgs e)
{
//在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式
//设置为 StateServer 或 SQLServer,则不会引发该事件。
}
</script>
这个时候,程序在1秒钟就会自动通过线程去监听,保证到定义的时间点去执行任务
多思考,多创新,才是正道!