Net Core教程

C# Quartz 定时器,启动,暂停,恢复任务

本文主要是介绍C# Quartz 定时器,启动,暂停,恢复任务,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

public class TestController : ControllerBase
{
static IScheduler _scheduler = null;
public TestController()
{
ISchedulerFactory factory = new StdSchedulerFactory();
_scheduler = factory.GetScheduler().Result;
}
[HttpGet]
public async Task Quartz_Start()
{
var schedulerFactory = new StdSchedulerFactory();
var scheduler = await schedulerFactory.GetScheduler();
await scheduler.Start();
Console.WriteLine($"任务调度器已启动");

//创建作业和触发器
var jobDetail = JobBuilder.Create<FirstJob>()
.WithIdentity("FirstJob")
.Build();

var trigger = TriggerBuilder.Create()
.WithSimpleSchedule(m =>
{
m.WithIntervalInSeconds(1).RepeatForever();
})
.Build();
//添加调度
await scheduler.ScheduleJob(jobDetail, trigger);
}

/// <summary>
/// 暂停一个job
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task Quartz_Pause()
{
//暂停使用
var jobKey = new JobKey("FirstJob");
if (await _scheduler.CheckExists(jobKey))
{
await _scheduler.PauseJob(jobKey);
}
}
/// <summary>
/// 恢复一个job
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task Quartz_Resume()
{
//恢复使用
var jobKey = new JobKey("FirstJob");
if (await _scheduler.CheckExists(jobKey))
{
await _scheduler.ResumeJob(jobKey);
}
}
}

这篇关于C# Quartz 定时器,启动,暂停,恢复任务的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!