Dispenser/DispenserCommon/Scheduler/PollingTask.cs

35 lines
794 B
C#

namespace DispenserCommon.Scheduler;
/// <summary>
/// 轮询任务
/// </summary>
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; }
/// <summary>
/// 移除任务进行定时任务释放
/// </summary>
public void Dispose()
{
_timer.Dispose();
}
/// <summary>
/// 定义一个虚方法, 子类可以重写该方法实现具体的轮询逻辑
/// </summary>
public virtual void Run()
{
}
}