3

I have a C# service and I need to run a function once a week.

I have a working C# service which currently is running on a timer every 60 seconds. Please see below a section of the services OnStart function:

// Set up a timer to trigger.  
System.Timers.Timer timer = new System.Timers.Timer
{
    Interval = 60000 //*1000; // 60 second  
};
timer.Elapsed += delegate {
    // Runs the code every 60 seconds but only triggers it if the schedule matches
    Function1();
};
timer.Start();

The above code calls Function1() every 60 seconds and I am checking it in Function1 if the current dayofweek and time matches the schedule and if it does than execute the rest of the function. Although this does work it not the most elegant way IMO.

I have tried using Quartz.net as it was looking promising but when I used all the examples available online (questions answered some 7 years ago in 2012), it is showing as an error in visual studio:

using System;
using Quartz;

public class SimpleJob : IJob
{

    public void Execute(IJobExecutionContext context)
    {
        throw new NotImplementedException();
    }
}

This is erroring

(Error CS0738 'SimpleJob' does not implement interface member 'IJob.Execute(IJobExecutionContext)'. 'SimpleJob.Execute(IJobExecutionContext)' cannot implement 'IJob.Execute(IJobExecutionContext)' because it does not have the matching return type of 'Task'.)

but this does not:

public Task Execute(IJobExecutionContext context)
{
    throw new NotImplementedException();
}

Could someone give a current working example of a job scheduled through Quartz.net for a beginner? Or using another elegant method than Quartz.net in a C# service?

G.Dimov
  • 2,173
  • 3
  • 15
  • 40
elemer82
  • 170
  • 1
  • 9

1 Answers1

4

First of all we need to implement a job implementation. For example:

internal class TestJob : IJob
{
    public Task Execute(IJobExecutionContext context)
    {

        Console.WriteLine("Job started");
        return Task.CompletedTask;
    }
}

Now we need to write a method which will return a Scheduler of Quartz :

    static async Task TestScheduler()
    {
        // construct a scheduler factory
        NameValueCollection props = new NameValueCollection
        {
            { "quartz.serializer.type", "binary" }
        };
        StdSchedulerFactory factory = new StdSchedulerFactory(props);

        // get a scheduler
        IScheduler sched = await factory.GetScheduler();
        await sched.Start();

        // define the job and tie it to our HelloJob class
        IJobDetail job = JobBuilder.Create<TestJob>()
            .WithIdentity("myJob", "group1")
            .Build();

        // Trigger the job to run now, and then every 40 seconds
        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("myTrigger", "group1")
            .StartNow()
            .WithSimpleSchedule(x => x
                .WithIntervalInMinutes(1)
                .RepeatForever())
            .Build();

        await sched.ScheduleJob(job, trigger);

    }

and in the Main method of the Program we will need to write following code:

    static async Task Main()
    {
        Console.WriteLine("Test Scheduler started");

        await TestScheduler();

        Console.ReadKey();
    }

Now this will keep executing after every minute.

Hope it helps.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • Great example, this got me up and running unlike the other numerous examples and documentation. – J_L Jan 17 '21 at 20:04