我有一个具有无限while循环并调用Quartz.Net调度程序的方法:
public static void Run()
{
var cron = configuration[Enums.ExecutionTime];
while (true)
{
if (!isRunning)
{
isRunning = true;
int secondsForSleep = Convert.ToUInt16(configuration[Enums.SleepTime]);
try
{
Execute(cron);
}
catch (Exception ex)
{
}
finally
{
isRunning = false;
Thread.Sleep(secondsForSleep);
}
}
}
}
execute方法如下:
public static async void Execute(string cron)
{
NameValueCollection props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
};
StdSchedulerFactory factory = new StdSchedulerFactory(props);
IScheduler sched = await factory.GetScheduler();
await sched.Start();
IJobDetail job = JobBuilder.Create<Job>()
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithCronSchedule(cron)
.ForJob(job.Key)
.Build();
await sched.ScheduleJob(job, trigger);
}
这是我的Job
类,其中包含要执行的过程:
[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class Job : IJob
{
public async Task Execute(IJobExecutionContext context)
{
var process = container.Resolve<Process.Process>();
try
{
var t = new Task(() =>
{
Console.WriteLine("Started");
process.MainProcess();
});
t.Start();
t.Wait();
Console.WriteLine("Finished");
await Task.Delay(360000);
}
catch (Exception e)
{
throw e;
}
finally
{
// clean
}
}
}
我的问题是,cron
被声明为仅在特定时间执行,而那时在作业类中的_process.Mainprocess()
应该仅被执行 ,但是,当我使用Console.WriteLine("Started");
并查看我的数据库时,我看到该过程被多次使用。这是asynchronous
方法的问题吗?我该如何停止这种行为,并在该时间仅一次调用它,然后等到该时间再次出现?