如何创建一次只能在C#中执行一次的cron计划?

时间:2015-02-26 07:18:17

标签: c# .net cron quartz-scheduler cronexpression

我在C#中使用了cron schedule来创建一个应该只触发一次作业的应用程序。这段代码抛出异常, Quartz.dll

中发生了未处理的“Quartz.SchedulerException”类型异常

以下是我的代码:

class Program
{
    static void Main(string[] args)
    {
        Test();
    }

    public static void Test()
    {
        ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
        IScheduler scheduler = schedulerFactory.GetScheduler();

        IJobDetail jobDetail = JobBuilder.Create<SatellitePaymentGenerationJob>()
            .WithIdentity("TestJob")
            .Build();
        Console.WriteLine(DateBuilder.DateOf(16, 30, 00, 24, 2, 2015));
        //ITrigger trigger = TriggerBuilder.Create()
        //    .ForJob(jobDetail)
        //    .WithCronSchedule("0 0 12 20 4 ? *")
        //    .WithIdentity("TestTrigger")
        //    .StartNow()
        //    .Build();
        ITrigger trigger = TriggerBuilder.Create()
                             .WithDescription("Once")
                             .WithSimpleSchedule(x => x.RepeatForever().WithRepeatCount(1))
                             .StartAt(DateBuilder.DateOf(12, 43, 00, 26, 2, 2015))
                             .Build();

        scheduler.ScheduleJob(jobDetail, trigger);
        scheduler.Start();
    }
}

internal class SatellitePaymentGenerationJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Console.WriteLine("test");
    }
}

我相信我做调度只执行一次的方式导致问题。请指教。

1 个答案:

答案 0 :(得分:0)

  ITrigger trigger = TriggerBuilder.Create()
                               .WithDescription("Once")
                               .WithSimpleSchedule(x => x
                               .WithIntervalInSeconds(40)
                               .RepeatForever())
                               .StartAt(DateBuilder.DateOf(14, 41, 00, 26, 2, 2015))
                               .EndAt(DateBuilder.DateOf(15, 14, 20))
                               .Build();