Worker.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. namespace wms.workservice
  2. {
  3. public class Worker : BackgroundService
  4. {
  5. private readonly ILogger<Worker> _logger;
  6. private readonly ConService _consevice;
  7. public Worker(ILogger<Worker> logger, ConService consevice)
  8. {
  9. _logger = logger;
  10. _consevice= consevice;
  11. }
  12. public override async Task StartAsync(CancellationToken cancellationToken)
  13. {
  14. _consevice.WriteLog("启动时间为: " + DateTimeOffset.Now);
  15. //_consevice.FJConsumer();
  16. await base.StartAsync(cancellationToken);
  17. }
  18. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  19. {
  20. while (!stoppingToken.IsCancellationRequested)
  21. {
  22. _consevice.WriteLog("执行时间为: " + DateTimeOffset.Now);
  23. _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
  24. await Task.Delay(10000, stoppingToken);
  25. }
  26. }
  27. //停止时执行
  28. public override async Task StopAsync(CancellationToken cancellationToken)
  29. {
  30. _consevice.WriteLog("停止时间为: " + DateTimeOffset.Now);
  31. await base.StopAsync(cancellationToken);
  32. }
  33. }
  34. }