gokhancnr gokhancnr - 1 month ago 16
C# Question

Quartz .NET Scheduler v2.4.1 stops after 10 repeats

I have a simple Console Application which is using Quartz .NET Scheduler v2.4.1
and it's simply writing actual time.

When I build my app it's only working 10 times and then stops.

enter image description here

class Program
{
static void Main(string[] args)
{
MySchedule.GetValue();
}
}

public static class MySchedule
{
public static void GetValue()
{
var scheduler = StdSchedulerFactory.GetDefaultScheduler();

scheduler.Start();

var job = JobBuilder.Create<MyJob>().Build();

var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(builder =>

builder.WithIntervalInSeconds(1)
.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(9, 40, 0))).Build();

scheduler.ScheduleJob(job, trigger);
}
}

public class MyJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine($"{DateTime.Now.ToString(new CultureInfo("en"))}");
Console.ReadLine();
}
}


I took a look at config file to change threadCount but it didn't work for me.

Any help would be greatful.

Thanks in advance.

Answer

Your program does not wait at all. Your Main method just ends. Make it wait if your want the program to run longer. For example with a Console.ReadLine. You can then remove the user interaction from your job. You may also want to have a handle on the scheduler while the program is running, I don't know what happens if the garbage collector figures out you have dropped the last reference to it.

class Program
{
    static void Main(string[] args)
    {
        var scheduler = MySchedule.GetValue();

        Console.WriteLine("Scheduler started. Press [Enter] to quit.");
        Console.ReadLine();
    }
}

public static class MySchedule
{
    public static IScheduler GetValue()
    {
        var scheduler = StdSchedulerFactory.GetDefaultScheduler();

        scheduler.Start();

        var job = JobBuilder.Create<MyJob>().Build();

        var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(builder =>

        builder.WithIntervalInSeconds(1)
        .OnEveryDay()
        .StartingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(9, 40, 0))).Build();

        scheduler.ScheduleJob(job, trigger);

        return scheduler;
    }
}

public class MyJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Console.WriteLine(DateTime.Now.ToString(new CultureInfo("en")));
    }
}