username@email.com
2021-06-21 83280d90d12545d36a301c437c7d9095f190aac3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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();
        }
    }
}