编程语言
首页 > 编程语言> > 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#,await,var,Quartz,scheduler,jobKey,public
来源: https://www.cnblogs.com/wwhuyulin/p/15904793.html