惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
宝玉的分享
宝玉的分享
I
InfoQ
P
Privacy International News Feed
V
V2EX
IT之家
IT之家
S
SegmentFault 最新的问题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V2EX - 技术
V2EX - 技术
C
CERT Recently Published Vulnerability Notes
C
Check Point Blog
The Register - Security
The Register - Security
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
AWS News Blog
AWS News Blog
M
MIT News - Artificial intelligence
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
B
Blog
N
Netflix TechBlog - Medium
B
Blog RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
T
Threatpost
Forbes - Security
Forbes - Security
U
Unit 42
A
Arctic Wolf
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Palo Alto Networks Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recorded Future
Recorded Future
L
Lohrmann on Cybersecurity
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
月光博客
月光博客
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
I
Intezer
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
The Hacker News
The Hacker News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
MyScale Blog
MyScale Blog
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位

博客园 - wuty007

C# 范围运算符 C# 调用WGC 实现桌面屏幕的捕获 完善基于WPF开发的标尺控件(含实例代码) C# 依赖注入 Microsoft.Extensions.DependencyInjection 实现 控制反转(IOC) C# 获取Windows系统的设备名称 记录 Windows系统开启hyper-v ,部分端口被保留,导致端口不能使用而报错的问题 WPF 调用 Win32的SetWindowDisplayAffinity 函数 实现捕获屏幕时,过滤指定的窗口 记录WPF 在清单列表设置了UIACESS为true,没有签名的报错“从服务器返回了一个参照” WPF 的ListBox 去除默认的Item项的 鼠标hover的背景颜色 WPF 调用 ChangeWindowMessageFilterEx 修改指定窗口 (UIPI) 消息筛选器的用户界面特权隔离 记录一下 WPF进程 SendMessage 发送窗口消息进行进程间通信,存在进程权限无法接受消息的问题 记录 使用PsExec启动System权限的WPF 程序 记录 命令行的 findstr 的使用 排查Windows 下的内存使用率过高,但是任务管理器看不到进程 Everything 支持 多实例 运行 指定应用 在 控制面板或者 设置的安装应用 置灰或隐藏卸载按钮 WPF 通过RawInput 获取 系统全局触摸事件 记录一下Windows系统下的命令行参数的字符个数限制 WPF 实现支持动态调整高度的文本显示控件
C# 定时任务 Quartz.NET 的使用
wuty007 · 2025-08-24 · via 博客园 - wuty007

一、定时任务的介绍

相信我们在生活中,大部分都会使用到定时任务去执行自定义的业务逻辑,如:每天早上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