第二讲:简单的实例,看看Quartz.Net强在哪里?
直接上代码,代码里有注释:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
using
System;
using
Quartz;
using
Quartz.Impl;
namespace
Lession01
{ class
Program
{
static
void Main( string [] args)
{
// 构造一个任务调度工厂
var
schedFact = new
StdSchedulerFactory();
//获取一个任务调度实例
var
sched = schedFact.GetScheduler();
sched.Start();
//定义一个工作
var
job = JobBuilder.Create<HelloJob>()
.WithIdentity( "job01" , "group01" )
.Build();
//定义一个触发器
var
trigger = TriggerBuilder.Create()
.WithIdentity( "myTrigger" , "group01" )
.StartNow()
.WithSimpleSchedule(
x => x.WithIntervalInSeconds(5)
.RepeatForever()
).Build();
sched.ScheduleJob(job, trigger);
}
}
public
class HelloJob : IJob
{
public
void Execute(IJobExecutionContext context)
{
Console.WriteLine( "Hello Quartz.Net \t"
+ DateTime.Now);
}
}
} |
运行看效果:
说明:5s执行以下HelloJob中的Execute方法,so easy,妈妈再也不用担心我的学习!