WMS新增,自定义定时任务

This commit is contained in:
alex
2023-08-02 17:50:51 +08:00
parent 9b4e76192a
commit e685faca9e
7 changed files with 143 additions and 50 deletions

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JNPF;
using JNPF.Common.Extension;
using JNPF.Logging;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Extensions.Hosting;
using Tnb.WarehouseMgr.Entities.Dto.Inputs;
using Tnb.WarehouseMgr.Interfaces;
namespace Tnb.WarehouseMgr
{
/// <summary>
/// 定时任务
/// added by ly on 20230802
/// </summary>
public class TimedTaskBackgroundService : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Run(() =>
{
CancellationTokenSource tokenSource = new();
CancellationToken token = tokenSource.Token;
var wareHouseService = App.GetRequiredService<IWareHouseService>();
TimedTask(withOutParamAction: (cts) => Task.Run(async () =>
{
while (!token.IsCancellationRequested)
{
await wareHouseService.GenTaskExecute(tokenSource);
await Task.Delay(1000);
}
}, token));
});
}
private Task? TimedTask(Func<CancellationTokenSource?, Task>? withOutParamAction = null, Func<TimedtaskInput, Task>? withParemAction = null, CancellationTokenSource? cts = default, TimedtaskInput? parameter = null)
{
return parameter != null ? withParemAction?.Invoke(parameter) : withOutParamAction?.Invoke(cts);
}
}
}