WEB程序启动时开始执行某一定时任务,应用池回收或程序关闭后再次启动定时任务,在原本基础上继续执行(利用序列化,保存其持久性)
定时任务对象序列化存入数据库,需要时再从数据库取出反序列化为一实例对象进行使用。
序列化,反序列化对象实例,byte[]之间的转换方法:
- /// <summary>
- /// 序列化一个类实例对象
- /// </summary>
- /// <param name="obj">类实例对象</param>
- /// <returns>序列化后的byte数组</returns>
- public staticbyte[] ClassSerializer(object obj)
- {
- //创建一内存流
- MemoryStream stream = new MemoryStream();
- //实例化二进制序列类
- IFormatter bf = new BinaryFormatter();
- //将给定类实例对象序列化到指定流
- bf.Serialize(stream, obj);
- //将流中内容读入到byte[]对象
- byte[] info = new byte[stream.Length];
- stream.Position = 0;
- stream.Read(info, 0, (int)stream.Length);
- //关闭流
- stream.Close();
- //返回序列化后的byte数组
- return info;
- }
- /// <summary>
- /// 反序列化流构建一个新对象
- /// </summary>
- /// <param name="info">序列化后的byte数组</param>
- /// <returns></returns>
- public staticobject ClassDeserialize(Byte[] info)
- {
- //创建一内存流用指定byte[]初始化
- MemoryStream stream = new MemoryStream(info);
- //实例化二进制序列类
- IFormatter bf = new BinaryFormatter();
- //反序列化流中数据,生成新的对象
- Object obj = bf.Deserialize(stream);
- //关闭流
- stream.Close();
- //返回反序列化后生成的对象
- return obj;
- }
基础定时任务操作类:
- namespace _66_TimerTask
- {
- //此类可序列化
- [Serializable]
- public class TimerInfo
- {
- //定义委托类
- public delegatevoid TimerTaskDelegate();
- //当前执行程序名称
- public string name ="";
- //几秒后再执行任务
- public int second;
- //定义委托实例
- private TimerTaskDelegate fun;
- //定义一个线程
- //线程是不可序列化
- [NonSerialized]
- private Thread t;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="name">线程名称</param>
- /// <param name="second">几秒后再执行任务</param>
- /// <param name="fun">要执行的任务</param>
- public TimerInfo(string name,int second, TimerTaskDelegate fun)
- {
- this.name = name;
- this.second = second;
- this.fun = fun;
- }
- /// <summary>
- /// 启动定时任务
- /// </summary>
- public void Star()
- {
- t = new Thread(NewThreadStar);
- t.Name = this.name;
- t.Start();
- }
- /// <summary>
- /// 线程调用的方法
- /// </summary>
- private void NewThreadStar()
- {
- //要执行的目标方法
- fun();
- //当前线程挂起指定时间
- Thread.Sleep(second*1000);
- //递归调用实现循环
- NewThreadStar();
- }
- /// <summary>
- /// 终止定时任务
- /// </summary>
- public void Abort()
- {
- if (t != null)
- {
- t.Abort();
- }
- }
- }
- }
定时任务调用的方法:
- namespace _66_TimerTask.DelegateFunction
- {
- [Serializable]
- public class DelegateFun
- {
- /// <summary>
- /// 测试用方法
- /// </summary>
- public staticvoid Test1()
- {
- SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=GlobeChineseClubV1.1;UID=sa;PWD=");
- con.Open();
- SqlCommand cmd = new SqlCommand("insert into TimerTest (time) values ('" + DateTime.Now +"')", con);
- cmd.ExecuteNonQuery();
- con.Close();
- }
- public staticvoid Test2()
- {
- SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=GlobeChineseClubV1.1;UID=sa;PWD=");
- con.Open();
- SqlCommand cmd = new SqlCommand("insert into TimerTest2 (time) values ('" + DateTime.Now +"')", con);
- cmd.ExecuteNonQuery();
- con.Close();
- }
- }
- }
测试(Global.asax):
- TimerInfo t1;
- void Application_Start(object sender, EventArgs e)
- {
- //在应用程序启动时运行的代码
- CreateTimer();
- }
- //创建定时任务
- private void CreateTimer()
- {
- #region 检查数据库中是否存储有序列化后的相关类实例对象
- //如存在,查询数据库指定字段,反序列化为指定实例对象:t1 = (TimerInfo)SerializationHelper.ClassDeserialize((byte[])sdr["info"]);
- //不存在,创建新的TimerInfo实例对象:t1 = new _66_TimerTask.TimerInfo(unCreateTimersName [i], 10, _66_TimerTask.DelegateFunction.DelegateFun.Test1);
- #endregion
- //启动定时任务
- t1.Star();
- }
- void Application_End(object sender, EventArgs e)
- {
- //在应用程序关闭时运行的代码
- //序列化指定实例对象,返回其BYTE[]
- byte[] info1 = SerializationHelper.ClassSerializer(t1);
- SqlConnection con = new SqlConnection(connectionStr);
- con.Open();
- //序列化对象后的BYTE[]写入数据库info字段(image类型)
- SqlCommand cmd = new SqlCommand("delete TimerInfoTest;insert into TimerInfoTest (className,info) values ('" + t1.name +"', @info)", con);
- cmd.Parameters.Add("@info", System.Data.SqlDbType.Binary);
- cmd.Parameters["@info"].Value = info1;
- cmd.ExecuteNonQuery();
- con.Close();
- }