支持Cron表达式、间隔时间的工具(TaskScheduler)
2024-09-01 01:50:41
后台任务如何支持间隔时间、Cron表达式两种方式?
分享一个项目TaskScheduler,这是我从Furion项目中拷出来的
开始
间隔时间后台服务
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);
}
}
}
最新文章
- python-socket模块
- vc++ 中 IntelliSense: 无法打开 源 文件 ";xxx.h";
- jquery属性过滤选择器
- SQL Server 存储(8/8):理解数据文件结构
- SQL判断语句用法和多表查询
- Myeclipse中java文件注释格式设置
- 计算机语言学习导论[C/C++]
- Practical Common Lisp
- WCF(1)----服务创建
- 微信内置浏览器私有接口WeixinJSBridge介绍(转)
- Linux搭建FastFDFS文件管理系统搭建,部署及上传材料
- 201521123001《Java程序设计》第2周学习总结
- java中List对象的操作方法
- WebSocket(一)-RFC6455
- hdu-1814(2-sat)
- c# Bitmap byte[] Stream 文件相互转换
- 20155205 2016-2017-2 《Java程序设计》第8周学习总结
- 排序算法(8)--Merge Sorting--归并排序--Merge sort--归并排序
- c++ 栈(顺序表)
- React Native踩坑之The SDK directory &#39;xxxxx&#39; does not exist
热门文章
- MapReduce框架原理--Shuffle机制
- Vue实现多文件上传功能(前端 + 后端代码)
- 基于ivy的源代码调试方法
- Git-06-远程仓库
- 【笔记】matplotilb数据可视化基础
- SQL 练习37
- 算法入门 - 基于动态数组的栈和队列(Java版本)
- 使用npm安装 Ant Design Vue 时报错—ant-design-vue@latest(sha1-qsf / gCIFcRYxyGmOKgx7TmHf1z4 =)seems to be corrupted.
- .NET Core程序发布报错:project.assets.json”没有“.NETCoreApp,Version=v3.1/win-x64”的目标。确保已运行还原,且“netcoreapp3.1”已包含在项目的 TargetFrameworks中。
- C#托管堆和垃圾回收