增加定时任务
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
|
||||
namespace JY.Model.Common
|
||||
{
|
||||
public class ScheduledTask : IDisposable
|
||||
{
|
||||
private Timer _timer;
|
||||
private readonly TimerCallback _callback;
|
||||
private readonly object _state;
|
||||
|
||||
public Guid TaskId { get; }
|
||||
public int Interval { get; private set; } // 毫秒
|
||||
public bool IsRunning { get; private set; }
|
||||
|
||||
public ScheduledTask(Guid taskId, TimerCallback callback, int interval, object state = null)
|
||||
{
|
||||
TaskId = taskId;
|
||||
_callback = callback;
|
||||
Interval = interval;
|
||||
_state = state;
|
||||
IsRunning = false;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
if (IsRunning) return;
|
||||
_timer = new Timer(_callback, _state, 0, Interval);
|
||||
IsRunning = true;
|
||||
}
|
||||
|
||||
public void ChangeInterval(int newInterval)
|
||||
{
|
||||
Interval = newInterval;
|
||||
if (IsRunning && _timer != null)
|
||||
{
|
||||
_timer.Change(0, newInterval);
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
if (!IsRunning) return;
|
||||
_timer?.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
IsRunning = false;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Stop();
|
||||
_timer?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JY.Model.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// 定时任务管理器
|
||||
/// </summary>
|
||||
public class TaskScheduler : IDisposable
|
||||
{
|
||||
private readonly ConcurrentDictionary<Guid, ScheduledTask> _tasks
|
||||
= new ConcurrentDictionary<Guid, ScheduledTask>();
|
||||
|
||||
/// <summary>
|
||||
/// 添加一个新任务
|
||||
/// </summary>
|
||||
public Guid AddTask(TimerCallback callback, int interval, object state = null)
|
||||
{
|
||||
var taskId = Guid.NewGuid();
|
||||
var task = new ScheduledTask(taskId, callback, interval, state);
|
||||
_tasks[taskId] = task;
|
||||
task.Start();
|
||||
return taskId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除一个任务
|
||||
/// </summary>
|
||||
public bool RemoveTask(Guid taskId)
|
||||
{
|
||||
if (_tasks.TryRemove(taskId, out var task))
|
||||
{
|
||||
task.Dispose();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改任务间隔
|
||||
/// </summary>
|
||||
public bool ChangeInterval(Guid taskId, int newInterval)
|
||||
{
|
||||
if (_tasks.TryGetValue(taskId, out var task))
|
||||
{
|
||||
task.ChangeInterval(newInterval);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止所有任务
|
||||
/// </summary>
|
||||
public void StopAll()
|
||||
{
|
||||
foreach (var task in _tasks.Values)
|
||||
{
|
||||
task.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
StopAll();
|
||||
foreach (var task in _tasks.Values)
|
||||
{
|
||||
task.Dispose();
|
||||
}
|
||||
_tasks.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user