C# 定时任务 Quartz.NET 的使用

news/2025/10/13 9:11:02/文章来源:https://www.cnblogs.com/mq0036/p/19137690

C# 定时任务 Quartz.NET 的使用

一、定时任务的介绍

相信我们在生活中,大部分都会使用到定时任务去执行自定义的业务逻辑,如:每天早上8点钟发送一份汇总好的财经报告到指定人的邮箱;或者每周一5点30分钟自动执行下载器下载电影,下载完并通过QQ等机器人的方式通知管理员(如下图)。

image

  二、C# 的Quartz.NET的使用

1、NuGet页面搜索Quartz.NET,并安装

image

2、创建一个 TestJob ,并对 IJob 的接口的实现

复制代码
/// <summary>
/// 创建一个测试的Job类
/// </summary>
public class TestJob : IJob
{public async Task Execute(IJobExecutionContext context){Console.WriteLine($"{DateTime.Now.ToString("yy-MM-dd HH:mm:ss fff")},执行了TestJob");await Task.CompletedTask;}
}
复制代码

3、实例化调度器的参数:任务明细,注意:“myGroup” 是任务的一个标识,每一个任务都有独立的一个标识状态

IJobDetail job = JobBuilder.Create<TestJob>().WithIdentity("TestJob", "myGroup").Build();

4、实例化调度器的参数:触发器,如下代码:创建一个一秒循环的触发器

复制代码
ITrigger trigger = TriggerBuilder.Create().WithIdentity("TestJobTrigger", "myGroup").WithSimpleSchedule(x =>{x.WithIntervalInSeconds(1).RepeatForever();}).Build();
复制代码

5、创建任务调度器,并执行任务

复制代码
StdSchedulerFactory factory = new StdSchedulerFactory();
//创建任务调度器
IScheduler scheduler = await factory.GetScheduler();
await scheduler.Start();
await scheduler.ScheduleJob(job, trigger);
Console.WriteLine("任务调度器已启动,按任意键退出...");
复制代码

6、执行的效果如下:

image

 三、时间表达式 Cron 的使用

官网说明:CronTrigger Tutorial | Quartz.NET

A cron expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of the allowed special characters for that field. The fields are as follows:

Field NameMandatoryAllowed ValuesAllowed Special Characters
Seconds YES 0-59 , - * /
Minutes YES 0-59 , - * /
Hours YES 0-23 , - * /
Day of month YES 1-31 , - * ? / L W
Month YES 1-12 or JAN-DEC , - * /
Day of week YES 1-7 or SUN-SAT , - * ? / L #
Year NO empty, 1970-2099 , - * /

So cron expressions can be as simple as this: * * * * ? *

or more complex, like this: 0/5 14,18,3-39,52 * ? JAN,MAR,SEP MON-FRI 2002-2010

 

 

Here are some full examples:

ExpressionMeaning
0 0 12 * * ? Fire at 12pm (noon) every day
0 15 10 ? * * Fire at 10:15am every day
0 15 10 * * ? Fire at 10:15am every day
0 15 10 * * ? * Fire at 10:15am every day
0 15 10 * * ? 2005 Fire at 10:15am every day during the year 2005
0 * 14 * * ? Fire every minute starting at 2pm and ending at 2:59pm, every day
0 0/5 14 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
0 0/5 14,18 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
0 0-5 14 * * ? Fire every minute starting at 2pm and ending at 2:05pm, every day
0 10,44 14 ? 3 WED Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
0 15 10 ? * MON-FRI Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 * ? Fire at 10:15am on the 15th day of every month
0 15 10 L * ? Fire at 10:15am on the last day of every month
0 15 10 L-2 * ? Fire at 10:15am on the 2nd-to-last last day of every month
0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L 2002-2005 Fire at 10:15am on every last Friday of every month during the years 2002, 2003, 2004 and 2005
0 15 10 ? * 6#3 Fire at 10:15am on the third Friday of every month
0 0 12 1/5 * ? Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.
0 11 11 11 11 ? Fire every November 11th at 11:11am.

 

如:我想要每天上午10:15分执行一次的cron表达式:0 15 10 * * ?

复制代码
IJobDetail job = JobBuilder.Create<TestJob>().WithIdentity("TestJob", "myGroup").Build();ITrigger trigger = TriggerBuilder.Create().WithIdentity("TestJobTrigger", "myGroup").WithCronSchedule("0 15 10 * * ?").Build();StdSchedulerFactory factory = new StdSchedulerFactory();
//创建任务调度器
IScheduler scheduler = await factory.GetScheduler();
//启动任务调度器
await scheduler.Start();//将创建的任务和触发器条件添加到创建的任务调度器当中
await scheduler.ScheduleJob(job, trigger);Console.WriteLine("任务调度器已启动,按任意键退出...");
Console.ReadKey();
复制代码

 

Demo 链接:wutyDemo/TimeTask at main · wutangyuan/wutyDemo

 

 

2025-10-13 09:03:59【出处】:https://www.cnblogs.com/wuty/p/19055973

=======================================================================================

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/935885.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2025.10.13——1橙

普及- P9752 [CSP-S 2023] 密码锁 虽然只是小模拟,但确实是真题,有点感觉。

WPF 通过RawInput获取系统全局触摸事件

WPF 通过RawInput获取系统全局触摸事件在做大屏或者平板的业务,或多或少会有监听触摸事件的需求。在WPF 上,监听自身应用的触摸事件是很简单的,可以监听 Windows的 Stylus、Touch、甚至是 Mouse的事件来实现业务逻辑…

基于高频电流探头的电磁兼容(EMI/EMC)测试与诊断技术方案

电磁兼容性(EMI/EMC)测试是确保电子设备在复杂电磁环境中可靠运行的重要环节。高频电流探头采用非侵入式测量方式,能够精准地捕捉电缆上的噪声电流,为诊断和解决电磁干扰问题提供可靠的数据支持。本文详细介绍了高…

Spring 事务、循环依赖连环问

Spring 事务 详情请查看:Spring 事务 Spring 事务实现方式有哪些? 事务就是一系列的操作原子执行。Spring事务机制主要包括声明式事务和编程式事务。编程式事务:通过编程的方式管理事务,这种方式带来了很大的灵活性…

20232327 2025-2026-1 《网络与系统攻防技术》实验一实验报告

20232327 2025-2026-1 《网络与系统攻防技术》实验一实验报告 1.实验内容 在本周的课程学习了缓冲区溢出和shellcode攻击的内容,以下是一些基本概念和解释:缓冲区:连续的一段存储空间; 缓冲区溢出攻击BOF(Buffer …

完整教程:OSPF LSA/ 路由种类

完整教程:OSPF LSA/ 路由种类2025-10-13 08:46 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !importan…

单挑市面上所有监控设备模拟器/可能是最好用的监控模拟器/支持onvif和28181协议/虚拟监控/桌面转监控/任意源转监控设备

一、前言说明 自从发布了这个监控设备模拟器,本意其实是卖代码,可是受欢迎程度不是程序员开发者,而是一堆非开发人员,没想到这个一个毫不起眼的需求,在外行人看来是真切实际的需求,比如一些收银台,需要把收银软…

在Java 11中,如何处理被弃用的类或接口?

在Java 11中处理被弃用的类或接口时,核心原则是使用官方推荐的替代方案,避免依赖过时API以确保代码的兼容性和可维护性。以下是具体处理方式和示例: 1. 替换内部API类(sun.* 或 com.sun.* 包下的类) 这些类属于JD…

chmod只修改文件或者只修改目录权限

chmod和chmod -R目录常用于修改文件,文件夹权限。加上-R参数会迭代的修改子目录和文件的权限。如果只想修改文件的权限,文件夹的权限不受影响。则可以使用下面的方法:chmod 750 `find /a /b -type f`会修改文件夹/a…

每周资讯 | 腾讯《三角洲行动》周年庆登双榜TOP1;腾讯首款生活模拟游戏《粒粒的小人国》曝光 - 教程

每周资讯 | 腾讯《三角洲行动》周年庆登双榜TOP1;腾讯首款生活模拟游戏《粒粒的小人国》曝光 - 教程pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: bloc…

.NET 自动依赖注入神器

在 .NET Core/Web 项目中,手动写一堆 services.AddScoped<...>、AddSingleton<...> 是否让你头大?今天给大家介绍一个神器——Injectio,帮你自动扫描并注册服务,减少重复代码,让你的依赖注入(DI)更…

NetDreamCTF WP - 指南

NetDreamCTF WP - 指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", &quo…

[1134] Connecting to Land Insight SFTP and GIS Servers

[1134] Connecting to Land Insight SFTP and GIS ServersHi Sir Bing,Greetings!Please be informed of your user credentials to servers. Also attached is the Work Instruction and PPK to connect to servers f…

VLA技术论文阅读 - 详解

VLA技术论文阅读 - 详解pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", &qu…

深入解析:246-基于Django的美食菜谱数据分析推荐系统

深入解析:246-基于Django的美食菜谱数据分析推荐系统pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas&qu…

PhotoShop网页版在线为图片加文字,制作个性海报教程

生活中,我们总有需要给图片加文字、或是亲手做一张个性海报的时候。你是不是也觉得用专业Photoshop太复杂?别担心,现在只要打开浏览器,进入在线修图平台,零基础也能快速上手,轻松做出创意十足的作品! 一、为什么…

实用指南:构建神经网络的两大核心工具

实用指南:构建神经网络的两大核心工具pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Mon…

简单高效的SQL注入测试方法:Break Repair技巧详解

本文详细介绍了SQL注入测试的简单有效方法,重点讲解Break & Repair技巧,包括数据库类型识别、盲注测试和信息提取等关键步骤,适合网络安全初学者和渗透测试人员学习参考。Break & Repair:我是如何以最简单…

实用指南:Qt 界面优化 --- QSS

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

display ip interface brief 概念及题目 - 指南

display ip interface brief 概念及题目 - 指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", &…