using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Logging;
|
using System;
|
using System.IO;
|
using System.Threading;
|
using System.Threading.Tasks;
|
|
namespace zhengcaioa.Timer
|
{
|
public class TimedBackgroundService : BackgroundService
|
{
|
private readonly ILogger _logger;
|
|
|
public TimedBackgroundService(ILogger<TimedBackgroundService> logger)
|
{
|
_logger = logger;
|
}
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
{
|
_logger.LogInformation("MyServiceA is starting.");
|
|
//stoppingToken.Register(() => File.Create($"E:\\dotnetCore\\Practice\\Practice\\{DateTime.Now.Millisecond}.txt"));
|
|
while (!stoppingToken.IsCancellationRequested)
|
{
|
_logger.LogInformation("MyServiceA 开始执行");
|
|
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
|
|
_logger.LogInformation("继续执行");
|
}
|
|
_logger.LogInformation("MyServiceA background task is stopping.");
|
}
|
|
public override void Dispose()
|
{
|
base.Dispose();
|
}
|
}
|
}
|