public ActionResult Index()
{
Run();
return View();
}
public async static Task Run()
{
// 1.创建scheduler的引用
ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler sched = await schedFact.GetScheduler();
//2.启动 scheduler
await sched.Start();
// 3.创建 job
IJobDetail job = JobBuilder.Create<SimpleJob>().WithIdentity("job1", "group1").Build();
// 4.创建 trigger
ITrigger trigger = TriggerBuilder.Create().WithIdentity("trigger1", "group1").WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()).Build();
// 5.使用trigger规划执行任务job
await sched.ScheduleJob(job, trigger);
}
/// <summary>
/// 任务
/// </summary>
public class SimpleJob : IJob
{
public virtual Task Execute(IJobExecutionContext context)
{
System.Diagnostics.Debug.WriteLine($"job工作了 在{DateTime.Now}");
return Console.Out.WriteLineAsync($"job工作了 在{DateTime.Now}");
}
}
参考:https://www.cnblogs.com/Vincent-yuan/p/10867277.html