增加定时任务

This commit is contained in:
liming 蔡
2026-07-21 19:37:29 +08:00
parent 127da277e0
commit af014ed33c
8 changed files with 182 additions and 20 deletions
File diff suppressed because one or more lines are too long
+55
View File
@@ -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();
}
}
}
+78
View File
@@ -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();
}
}
}
+3 -3
View File
@@ -67,6 +67,8 @@
<ItemGroup>
<Compile Include="BL.Entity\Chart24HourData.cs" />
<Compile Include="BL.Entity\RequestResult.cs" />
<Compile Include="Common\ScheduledTask.cs" />
<Compile Include="Common\TaskScheduler.cs" />
<Compile Include="DB.Entity\AbnormalVoice.cs" />
<Compile Include="DB.Entity\AlarmData.cs" />
<Compile Include="DB.Entity\CamInfor.cs" />
@@ -93,8 +95,6 @@
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Common\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>