youngfan007 发表于 2017-12-28 22:53:49

asp.net简单定时任务实现

public>
{  

#region 单例  

  
private static TimeTask _task = null;
  

  
public static TimeTask Instance
  
{
  
get
  
{
  
if (_task == null)
  
{
  
_task = new TimeTask();
  
}
  
return _task;
  
}
  
}
  

  
#endregion
  

  
//事件
  
public event System.Timers.ElapsedEventHandler ExecuteTask;
  

  
//时间对象
  
private System.Timers.Timer _timer = null;
  

  
//定义时间间隔
  
private int _interval = 1000;//默认1秒钟
  
public int Interval
  
{
  
set
  
{
  
_interval = value;
  
}
  
get
  
{
  
return _interval;
  
}
  
}
  

  
//开始
  
public void Start()
  
{
  
if (_timer == null)
  
{
  
_timer = new System.Timers.Timer(_interval);
  
_timer.Elapsed += new System.Timers.ElapsedEventHandler(_timerElapsed);
  
_timer.Enabled = true;
  
_timer.Start();
  
}
  
}
  

  
//委托方法,映射到传入的值
  
protected void _timerElapsed(object sender, System.Timers.ElapsedEventArgs e)
  
{
  
if (null != ExecuteTask)
  
{
  
ExecuteTask(sender, e);
  
}
  
}
  

  
//停止
  
public void Stop()
  
{
  
if (_timer != null)
  
{
  
_timer.Stop();
  
_timer.Dispose();
  
_timer = null;
  
}
  
}
  

  
}
页: [1]
查看完整版本: asp.net简单定时任务实现