当前位置:网站首页>.Net Core后台任务启停(BackgroundService)
.Net Core后台任务启停(BackgroundService)
2022-08-09 14:53:00 【iZaix】
oh no,有毒
说到定时任务,可能第一个会想到Quartz,但是想到需要更简洁,而且想要毫秒级的周期,这个Cron真是太不智慧了,虽说可以在单个任务中轮询,但是这个Trigger用起来是很不适应。那么找找源头,有什么简单的方法开启定时任务,然后大佬是这样说的 帮助 。
看起来很不错啊,只需要简单继承 BackgroundService 基类就可以了,先来做个测试,重写一下开始、结束、执行:
运行一下试试,很不错,看起来如此的简单。
但是为什么 停止不了!停止不了!停止不了!,只能求助网上各位大佬了,在此感谢
osc_aq3v6w0z的帮助 原文链接,
BackgroundService 中函数调用 ExecuteAsync 时给它赋的参数直接是一个内部的只读变量,你在外部调用,StartAsync 给它输入的参数根本就没有用到。
到底怎么回事
说到是BackgroundService 基类中并没有使用传进去的参数,那么来看看源码长什么样(开源福利)
// Copyright (c) .NET Foundation. Licensed under the Apache License, Version 2.0.
/// <summary>
/// Base class for implementing a long running <see cref="IHostedService"/>.
/// </summary>
public abstract class BackgroundService : IHostedService, IDisposable
{
private Task _executingTask;
private readonly CancellationTokenSource _stoppingCts = new CancellationTokenSource();
protected abstract Task ExecuteAsync(CancellationToken stoppingToken);
public virtual Task StartAsync(CancellationToken cancellationToken)
{
// Store the task we're executing
_executingTask = ExecuteAsync(_stoppingCts.Token);
// If the task is completed then return it,
// this will bubble cancellation and failure to the caller
if (_executingTask.IsCompleted)
{
return _executingTask;
}
// Otherwise it's running
return Task.CompletedTask;
}
public virtual async Task StopAsync(CancellationToken cancellationToken)
{
// Stop called without start
if (_executingTask == null)
{
return;
}
try
{
// Signal cancellation to the executing method
_stoppingCts.Cancel();
}
finally
{
// Wait until the task completes or the stop token triggers
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite,cancellationToken));
}
}
public virtual void Dispose()
{
_stoppingCts.Cancel();
}
}
确实,这就是问题的根源,那么看看怎么办
方法一:跳过StartAsync、StopAsync ,直接调用 ExecuteAsync ;
方法二:仿照官方的 BackgroundService,实现 IHostedService 接口,自己写一个BackgroundService
方法三:使用 BackgroundWorker
下面来尝试一下方法二,简单的件token替换一下
running…
边栏推荐
猜你喜欢
随机推荐
编译器不同,模式不同,对结果的影响
浅谈一下量化交易与程序化交易
MySQL 原理与优化:Limit 查询优化
OpenSSF的开源软件风险评估工具:Scorecards
redis从入门到精通
Mysql two engines comparison
docke安装mysql以及主从搭建(并且指定数据生成路径)
双摄像头系列原理深度剖析【转载】
What is the difference between the four common resistors?
Two-dimensional array to realize the eight queens problem
【超级账本开发者系列】专访——肖慧 : 不忘初心,方得始终
数据库多表链接查询的方式
[MySql]实现多表查询-一对一,一对多
docker安装nacos并且指定容器数据卷,数据库连接等
函数调用约定
Talking about Shallow Cloning and Deep Cloning of ArraryList
经典面试题 之 TCP 三次握手/ 四次挥手
常见的四种电阻之间有什么不同?
经典面试题 之 JVM调优
排序方法(希尔、快速、堆)









