增加定时任务

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
+3 -13
View File
@@ -152,7 +152,6 @@ namespace JY.Inspection
private ByteTransformBase TransformBase = new ByteTransformBase(); private ByteTransformBase TransformBase = new ByteTransformBase();
/// <summary> /// <summary>
/// 用于电芯出站时向FMS上报出站及生产数据 /// 用于电芯出站时向FMS上报出站及生产数据
/// </summary> /// </summary>
@@ -417,7 +416,6 @@ namespace JY.Inspection
#endregion #endregion
#region 页面事件 #region 页面事件
private void HomeForm_Load(object sender, EventArgs e) private void HomeForm_Load(object sender, EventArgs e)
{ {
@@ -471,6 +469,8 @@ namespace JY.Inspection
_speech.SelectVoice(neededVoice.VoiceInfo.Name); _speech.SelectVoice(neededVoice.VoiceInfo.Name);
} }
InitTaskScheduler();
LogManagerControl.AddLog("程序已启动", LogAddtype.local); LogManagerControl.AddLog("程序已启动", LogAddtype.local);
} }
@@ -867,7 +867,6 @@ namespace JY.Inspection
{ {
while (true) while (true)
{ {
if (mCOMState.Token.IsCancellationRequested) if (mCOMState.Token.IsCancellationRequested)
{ {
return; return;
@@ -875,8 +874,6 @@ namespace JY.Inspection
resetEventRecv.WaitOne(); resetEventRecv.WaitOne();
if (isEnabled) if (isEnabled)
{ {
if (master !=null) if (master !=null)
{ {
try try
@@ -885,7 +882,6 @@ namespace JY.Inspection
List<double> resylt_float = new List<double>(); List<double> resylt_float = new List<double>();
OperateResult<ushort[]> result = new OperateResult<ushort[]>(); OperateResult<ushort[]> result = new OperateResult<ushort[]>();
for (int i = 0; i < Global.Instructions.Count; i++) for (int i = 0; i < Global.Instructions.Count; i++)
{ {
ushort startAddress = (ushort)System.Convert.ToInt32(Global.Instructions[i], 16); ushort startAddress = (ushort)System.Convert.ToInt32(Global.Instructions[i], 16);
@@ -912,7 +908,7 @@ namespace JY.Inspection
resylt_float.Add((Convert.ToDouble(resylt[index++]) / 10)); resylt_float.Add((Convert.ToDouble(resylt[index++]) / 10));
//线电压UAC //线电压UAC
resylt_float.Add((Convert.ToDouble(resylt[index++]) / 10)); resylt_float.Add((Convert.ToDouble(resylt[index++]) / 10));
;
//电流IA //电流IA
resylt_float.Add((Convert.ToDouble(resylt[index++]) / 1000)); resylt_float.Add((Convert.ToDouble(resylt[index++]) / 1000));
//电流IB //电流IB
@@ -1402,9 +1398,6 @@ namespace JY.Inspection
FrmCCDQuery frm = new FrmCCDQuery(); FrmCCDQuery frm = new FrmCCDQuery();
frm.ShowDialog(); frm.ShowDialog();
} }
#endregion #endregion
#region PLC读写 #region PLC读写
@@ -1561,9 +1554,6 @@ namespace JY.Inspection
omronPLCCom.lstMcUI[i].IsRun = false; omronPLCCom.lstMcUI[i].IsRun = false;
omronPLCCom.lstMcUI[i].btnStart.Text = "启动"; omronPLCCom.lstMcUI[i].btnStart.Text = "启动";
omronPLCCom.lstMcUI[i].HeartBeat = false; omronPLCCom.lstMcUI[i].HeartBeat = false;
} }
} }
} }
+29
View File
@@ -0,0 +1,29 @@
using JY.Model;
using JY.Model.Common;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace JY.Inspection
{
public partial class HomeForm
{
#region 定时任务
TaskScheduler scheduler = null;
private void InitTaskScheduler()
{
if (scheduler == null)
{
scheduler = new TaskScheduler();
}
scheduler.AddTask(state => {
Debug.WriteLine("Hello");
}, interval: 1000 * 60);
}
#endregion
}
}
+3
View File
@@ -307,6 +307,9 @@
<Compile Include="Frm\ListViewBuff.cs"> <Compile Include="Frm\ListViewBuff.cs">
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>
<Compile Include="HomeFormExtend.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Mes\MESDataCombin.cs" /> <Compile Include="Mes\MESDataCombin.cs" />
<Compile Include="Mes\MesLog.cs" /> <Compile Include="Mes\MesLog.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
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> <ItemGroup>
<Compile Include="BL.Entity\Chart24HourData.cs" /> <Compile Include="BL.Entity\Chart24HourData.cs" />
<Compile Include="BL.Entity\RequestResult.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\AbnormalVoice.cs" />
<Compile Include="DB.Entity\AlarmData.cs" /> <Compile Include="DB.Entity\AlarmData.cs" />
<Compile Include="DB.Entity\CamInfor.cs" /> <Compile Include="DB.Entity\CamInfor.cs" />
@@ -93,8 +95,6 @@
<None Include="app.config" /> <None Include="app.config" />
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup />
<Folder Include="Common\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </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>