后台任务如何支持间隔时间、Cron表达式两种方式?

分享一个项目TaskScheduler,这是我从Furion项目中拷出来的

源码:https://gitee.com/dot-net-core/task-scheduler.git

开始

间隔时间后台服务

    public class IntervalBackgroundService : BackgroundService
{
private readonly ILogger<IntervalBackgroundService> _logger = null;
public IntervalBackgroundService(ILogger<IntervalBackgroundService> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await SpareTime.DoAsync(1000, () =>
{
_logger.LogInformation("Interval Worker running at: {time}", DateTimeOffset.Now);
}, stoppingToken);
}
}
}

Cron表达式后台服务

    public class CronBackgroundService : BackgroundService
{
private readonly ILogger<CronBackgroundService> _logger = null;
public CronBackgroundService(ILogger<CronBackgroundService> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// 执行 Cron 表达式任务
await SpareTime.DoAsync("*/5 * * * * *", () =>
{
_logger.LogInformation("Cron Worker running at: {time}", DateTimeOffset.Now);
}, stoppingToken, CronFormat.IncludeSeconds);
}
}
}

最新文章

  1. python-socket模块
  2. vc++ 中 IntelliSense: 无法打开 源 文件 &quot;xxx.h&quot;
  3. jquery属性过滤选择器
  4. SQL Server 存储(8/8):理解数据文件结构
  5. SQL判断语句用法和多表查询
  6. Myeclipse中java文件注释格式设置
  7. 计算机语言学习导论[C/C++]
  8. Practical Common Lisp
  9. WCF(1)----服务创建
  10. 微信内置浏览器私有接口WeixinJSBridge介绍(转)
  11. Linux搭建FastFDFS文件管理系统搭建,部署及上传材料
  12. 201521123001《Java程序设计》第2周学习总结
  13. java中List对象的操作方法
  14. WebSocket(一)-RFC6455
  15. hdu-1814(2-sat)
  16. c# Bitmap byte[] Stream 文件相互转换
  17. 20155205 2016-2017-2 《Java程序设计》第8周学习总结
  18. 排序算法(8)--Merge Sorting--归并排序--Merge sort--归并排序
  19. c++ 栈(顺序表)
  20. React Native踩坑之The SDK directory &#39;xxxxx&#39; does not exist

热门文章

  1. MapReduce框架原理--Shuffle机制
  2. Vue实现多文件上传功能(前端 + 后端代码)
  3. 基于ivy的源代码调试方法
  4. Git-06-远程仓库
  5. 【笔记】matplotilb数据可视化基础
  6. SQL 练习37
  7. 算法入门 - 基于动态数组的栈和队列(Java版本)
  8. 使用npm安装 Ant Design Vue 时报错—ant-design-vue@latest(sha1-qsf / gCIFcRYxyGmOKgx7TmHf1z4 =)seems to be corrupted.
  9. .NET Core程序发布报错:project.assets.json”没有“.NETCoreApp,Version=v3.1/win-x64”的目标。确保已运行还原,且“netcoreapp3.1”已包含在项目的 TargetFrameworks中。
  10. C#托管堆和垃圾回收