人机交互页面
This commit is contained in:
@@ -7,6 +7,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ZR.Service.mes.andon.Iservice;
|
||||
|
||||
namespace ZR.Service.mes.andon
|
||||
{
|
||||
@@ -14,7 +15,7 @@ namespace ZR.Service.mes.andon
|
||||
{
|
||||
private readonly ILogger<ScheduledBackgroundService> _logger;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly TimeSpan _interval = TimeSpan.FromMinutes(5);
|
||||
private readonly TimeSpan _interval = TimeSpan.FromMinutes(1);
|
||||
|
||||
public ScheduledBackgroundService(
|
||||
ILogger<ScheduledBackgroundService> logger,
|
||||
@@ -35,15 +36,16 @@ namespace ZR.Service.mes.andon
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation($"开始执行定时任务: {DateTime.Now:HH:mm:ss}");
|
||||
|
||||
//// 使用 Scope 获取 Scoped 服务
|
||||
//using (var scope = _scopeFactory.CreateScope())
|
||||
//{
|
||||
// var myService = scope.ServiceProvider.GetRequiredService<IMyBusinessService>();
|
||||
// await myService.ProcessDataAsync();
|
||||
//}
|
||||
|
||||
_logger.LogInformation($"开始执行安灯超时上报定时任务: {DateTime.Now:HH:mm:ss}");
|
||||
// 核心:创建作用域,获取Scoped服务
|
||||
using (var scope = _scopeFactory.CreateScope())
|
||||
{
|
||||
// 获取你的报警业务服务(Scoped生命周期)
|
||||
var alarmService = scope.ServiceProvider.GetRequiredService<AndonAlarmRecordService>();
|
||||
// 执行你的自动超时上报逻辑
|
||||
var result = alarmService.AlarmReportAuto();
|
||||
_logger.LogInformation($"定时任务执行完成,结果:{result.Msg}");
|
||||
}
|
||||
_logger.LogInformation($"定时任务完成");
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -52,9 +54,12 @@ namespace ZR.Service.mes.andon
|
||||
}
|
||||
|
||||
// 等待指定间隔
|
||||
await Task.Delay(_interval, stoppingToken);
|
||||
// Task.Delay增加取消令牌检查,避免任务卡住
|
||||
if (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(_interval, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("定时后台服务停止");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user