一:Nuget添加Quartz.net和Topshelf
二:新建HelloJob类继承IJob
public class HelloJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
Console.WriteLine("HelloJob is executing.");
}
}
三:调用
public static async Task Demo()
{
ISchedulerFactory schedFact = new StdSchedulerFactory();
// get a scheduler
IScheduler sched =await schedFact.GetScheduler();
sched.Start();
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("myJob", "group1")
.Build();
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(5)
.RepeatForever())
.Build();
sched.ScheduleJob(job, trigger);
}
这是一个简单的定时器,每5秒执行HelloJob方法
https://www.cnblogs.com/pingming/p/4999228.html
demo 下载:https://download.csdn.net/user/qq_27169469/uploads