尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

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

C# 定时任务 Quartz.NET 的使用
📅 发布时间:2026/6/19 10:19:54

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

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

您的资助是我最大的动力!
金额随意,欢迎来赏!
付款后有任何问题请给我留言。

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【关注我】。(●'◡'●)

因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!

如果对你有所帮助,赞助一杯咖啡!打           付款后有任何问题请给我留言!!!

本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/19137690

【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!

相关新闻

  • 基于高频电流探头的电磁兼容(EMI/EMC)测试与诊断技术方案
  • 20232327 2025-2026-1 《网络与系统攻防技术》实验一实验报告
  • NetDreamCTF WP - 指南

最新新闻

  • 2026苏州大额黄金回收测评|对公个人双合规,收的顶资金安全兜底 - 奢侈品回收测评
  • TC818A芯片实战指南:集成运放配置、电阻选型与LCD驱动优化
  • AI Agent正在改变企业:为什么执行型AI成为新的增长引擎
  • QML MediaPlayer实战:从零构建跨平台轻量视频播放器
  • GEO系统源码揭秘:杭州爱搜索如何重新定义AI搜索优化 - 品牌报告
  • 【干货】7套核心数据分析思维框架,搞定90%业务涨跌问题

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号