WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
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
43
44
45
46
47
using JiepeiWMS.Common.Helper;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
 
namespace JiepeiWMS.Tasks
{
    public class Job2TimedService : IHostedService, IDisposable
    {
        private Timer _timer;
 
        // 这里可以注入
        public Job2TimedService()
        {
        }
 
        public Task StartAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine("Job 2 is starting.");
 
            _timer = new Timer(DoWork, null, TimeSpan.Zero,
                TimeSpan.FromSeconds(60 * 60 * 2));//两个小时
 
            return Task.CompletedTask;
        }
 
        private void DoWork(object state)
        {
            ConsoleHelper.WriteWarningLine($"Job 2: {DateTime.Now}");
        }
 
        public Task StopAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine("Job 2 is stopping.");
 
            _timer?.Change(Timeout.Infinite, 0);
 
            return Task.CompletedTask;
        }
 
        public void Dispose()
        {
            _timer?.Dispose();
        }
    }
}