namespace DispenserCommon.Scheduler;
///
/// 轮询任务
///
public abstract class PollingTask : ITask
{
private readonly Timer _timer;
protected PollingTask(string name, int interval = 100)
{
Interval = interval;
Name = name;
_timer = new Timer(_ => { Run(); }, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(Interval));
}
public int Interval { get; set; }
public string Name { get; set; }
///
/// 移除任务进行定时任务释放
///
public void Dispose()
{
_timer.Dispose();
}
///
/// 定义一个虚方法, 子类可以重写该方法实现具体的轮询逻辑
///
public virtual void Run()
{
}
}