This commit is contained in:
72
Common/Time_TaskHelper.cs
Normal file
72
Common/Time_TaskHelper.cs
Normal file
@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Epost.Common
|
||||
{
|
||||
public class Time_TaskHelper
|
||||
{
|
||||
public event System.Timers.ElapsedEventHandler ExecuteTask;
|
||||
|
||||
private static readonly Time_TaskHelper _task = null;
|
||||
private System.Timers.Timer _timer = null;
|
||||
//定义时间
|
||||
private int _interval = 1000;
|
||||
public int Interval
|
||||
{
|
||||
set
|
||||
{
|
||||
_interval = value;
|
||||
}
|
||||
get
|
||||
{
|
||||
return _interval;
|
||||
}
|
||||
}
|
||||
|
||||
static Time_TaskHelper()
|
||||
{
|
||||
_task = new Time_TaskHelper();
|
||||
}
|
||||
|
||||
public static Time_TaskHelper Instance()
|
||||
{
|
||||
return _task;
|
||||
}
|
||||
|
||||
//开始
|
||||
public void Start()
|
||||
{
|
||||
if (_timer == null)
|
||||
{
|
||||
_timer = new System.Timers.Timer(_interval);
|
||||
_timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
|
||||
_timer.Enabled = true;
|
||||
_timer.Start();
|
||||
}
|
||||
}
|
||||
|
||||
protected void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
||||
{
|
||||
if (null != ExecuteTask)
|
||||
{
|
||||
ExecuteTask(sender, e);
|
||||
}
|
||||
}
|
||||
|
||||
//停止
|
||||
public void Stop()
|
||||
{
|
||||
if (_timer != null)
|
||||
{
|
||||
_timer.Stop();
|
||||
_timer.Dispose();
|
||||
_timer = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user