src/sample/XXX.API/StartTask/TimerjobStartTask .cs
(开头部分) 2KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using System.Threading;
namespace XXX.API.StartTask
{
/// <summary>
/// 通过初始化一次后的定时执行作业示例
/// 固定周期的定时器作业示例代码
/// </summary>
public class TimerjobStartTask //: IStartupTaskAsync
{
private readonly IConfiguration _configuration;
public TimerjobStartTask(IConfiguration configuration)
{
_configuration = configuration;
}
public int Order => 0;
public async Task ExecuteAsync()
{
//间隔3秒执行一次
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(3));
try
{
//不会发生重入,只允许有一个消费者
while (await timer.WaitForNextTickAsync())
{
Console.WriteLine($"【定时任务被触发】Tick {DateTime.Now}");
}
}
catch (OperationCanceledException)
{
Console.WriteLine("【定时任务被取消】Operation cancelled");
}
}
}
/// <summary>
/// 后台服务示例,3秒执行一次方法
/// 需在startup中注册services.AddHostedService<TimedHostedService>();
/// </summary>
public class TimedHostedService : BackgroundService
{
private readonly PeriodicTimer _timer;
private readonly ILogger<TimedHostedService> _logger;
public TimedHostedService(
ILogger<TimedHostedService> logger)
{
_logger = logger;
_timer = new PeriodicTimer(TimeSpan.FromSeconds(5));
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation(
$"Queued Hosted Service is running.{Environment.NewLine}" +
$"{Environment.NewLine}Tap W to add a work item to the " +
$"background queue.{Environment.NewLine}");
while (await _timer.WaitForNextTickAsync(stoppingToken))
后面还有 25 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。