纪念人类首张黑洞照片发布
![](https://raw.githubusercontent.com/ZZRRegion/Resource/master/2019%E5%B9%B44%E6%9C%8810%E6%97%A5/%E9%BB%91%E6%B4%9E.jpg)
第一种方式BackgroundService
基于后台服务类BackgroundService实现,类所在命名空间Microsoft.Extensions.Hosting;添加定时服务类,示例如下
public class ServerTimeA : BackgroundService { private readonly ILogger<ServerTimeA> logger; public ServerTimeA(ILogger<ServerTimeA> logger) { this.logger = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { this.logger.LogInformation("StartA"); while (!stoppingToken.IsCancellationRequested) { this.logger.LogWarning("DoworkA..."); await Task.Delay(2000, stoppingToken); } this.logger.LogInformation("EndA..."); } } public class ServerTimeB : BackgroundService { private readonly ILogger<ServerTimeB> logger; public ServerTimeB(ILogger<ServerTimeB> logger) { this.logger = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { this.logger.LogInformation("StartB"); while (!stoppingToken.IsCancellationRequested) { this.logger.LogWarning("DoworkB..."); await Task.Delay(3000, stoppingToken); } this.logger.LogInformation("EndB..."); } }
然后在Startup类中的ConfigureServices方法中添加
services.AddHostedService<ServerTimeA>();
services.AddHostedService<ServerTimeB>();
第二种方式quarzt
第三种方式Hangfire
首先安装Hangfire库
然后再Startup.cs的ConfigureServices函数里面注册服务
services.AddHangfire(config => { //使用SQLServer持久化存储 config.UseSqlServerStorage(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersAdministratorDocumentshaiwell.mdf;Integrated Security=True;Connect Timeout=30"); });
然后再Configure中添加
app.UseHangfireDashboard();//启用Hangfire控制面板 app.UseHangfireServer();//启用Hangfire服务
而后就可以访问localhost:5000/Hangfire来查看面板了
-
基于队列的任务处理
BackgroundJob.Enqueue(() => this.TestOne());//基于队列的任务处理
-
延迟任务执行
BackgroundJob.Schedule(() => this.TestSchedule(), TimeSpan.FromSeconds(1));
-
循环任务执行
RecurringJob.AddOrUpdate(() => this.TestAddOrUpdate(), Cron.Minutely());
-
继续在其父作业完成时执行
string id = BackgroundJob.Enqueue(() => Console.WriteLine("Hello"));
BackgroundJob.ContinueJobWith(id, () => Console.WriteLine("World!"));
</li> </ol> </div> </div> <h2 class="text-primary">第四种方式TimedJob</h2> <div> <p>Pomelo.AspNetCore.TimedJob是一个.Net Core实现的定时任务job库,支持毫秒级定时任务</p> <p>首先安装对应的包:Pomelo.AspNetCore.TimedJob</p> <p>然后在Startup.cs的ConfigureServices函数里面注册服务</p> <code>services.AddTimedJob();</code> <p>然后在Configure中添加</p> <code>app.UseTimedJob();</code> <p>添加一个定时执行类,继承Job类</p> <pre> public class AutoGetMovieListJob:Job { private readonly ILogger<AutoGetMovieListJob> logger; public AutoGetMovieListJob(ILogger<AutoGetMovieListJob> logger) { this.logger = logger; } /// <summary> /// Begin 起始时间;Interval执行时间间隔,单位是毫秒 /// SkipWhileExecuting是否等待上一个执行完成该,true为等待 /// </summary> [Invoke(Begin = "2019-04-10 20:40", Interval = 1000 * 3, SkipWhileExecuting = true)] public void Run() { this.logger.LogError($"开始执行了!{DateTime.Now}"); } [Invoke(Begin = "2019-04-10 20:45", Interval = 1000 * 4, SkipWhileExecuting = true)] public void TestA() { this.logger.LogError($"TestA 首张黑洞照片问世!{DateTime.Now}"); } } </pre> </div> </div> </div>