You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using Admin.Core.Common;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Admin.Core.Tasks
|
|
|
|
|
{
|
|
|
|
|
public class Job1TimedService : IHostedService, IDisposable
|
|
|
|
|
{
|
|
|
|
|
private Timer _timer;
|
|
|
|
|
|
|
|
|
|
// 这里可以注入
|
|
|
|
|
public Job1TimedService()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Job 1 is starting.");
|
|
|
|
|
|
|
|
|
|
_timer = new Timer(DoWork, null, TimeSpan.Zero,
|
|
|
|
|
TimeSpan.FromSeconds(1000));
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DoWork(object state)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Job 1 启动成功");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Error:{ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConsoleHelper.WriteSuccessLine($"Job 1: {DateTime.Now}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Job 1 is stopping.");
|
|
|
|
|
|
|
|
|
|
_timer?.Change(Timeout.Infinite, 0);
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_timer?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|