C#Quartz调度方法异步

时间:2018-03-29 01:42:01

标签: c# asp.net-mvc quartz-scheduler

我有async方法并计划每10分钟运行一次。但是,它只是第一次运行。

的Application_Start

protected void Application_Start()
{
    ...
    JobScheduler.Start().GetAwaiter().GetResult();
}

的jobscheduler

public static class JobScheduler
{
    public static async Task Start()
    {
        try
        {
            NameValueCollection props = new NameValueCollection
            {
                { "quartz.serializer.type", "binary" }
            };
            StdSchedulerFactory factory = new StdSchedulerFactory(props);
            IScheduler scheduler = await factory.GetScheduler();

            await scheduler.Start();

            IJobDetail job = JobBuilder.Create<Service>()
                .WithIdentity("job1", "group1")
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")
                .StartNow()
                .WithSimpleSchedule(x => x
                    .WithIntervalInMinutes(10)
                    .RepeatForever())
                .Build();

            await scheduler.ScheduleJob(job, trigger);
        }
        catch (SchedulerException se)
        {
            Console.WriteLine(se);
        }
    }
}

服务

public class Service : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        using (StreamWriter streamWriter = new StreamWriter(@"C:\pacote\IDGLog.txt", true))
        {
            await streamWriter.WriteLineAsync(DateTime.Now.ToString());
        }
    }
}

我不知道我需要做什么才能连续跑

2 个答案:

答案 0 :(得分:0)

我的 Spidey Senses 告诉我scheduler超出范围,Job运行一次因为StartNow所有内容都被清除

答案 1 :(得分:0)

尝试最后调用方法await scheduler.Start();。应该可以。