How to send argument to class in Quartz.Net

Hosein picture Hosein · Dec 13, 2012 · Viewed 19.4k times · Source

I'm using Quartz.Net (version 2) for running a method in a class every day at 8:00 and 20:00 (IntervalInHours = 12)

Everything is OK since I used the same job and triggers as the tutorials on Quartz.Net, but I need to pass some arguments in the class and run the method bases on those arguments.

Can any one help me how I can use arguments while using Quartz.Net?

Answer

Arsen Mkrtchyan picture Arsen Mkrtchyan · Dec 13, 2012

You can use JobDataMap

jobDetail.JobDataMap["jobSays"] = "Hello World!";
jobDetail.JobDataMap["myFloatValue"] =  3.141f;
jobDetail.JobDataMap["myStateData"] = new ArrayList(); 

public class DumbJob : IJob
{
    public void Execute(JobExecutionContext context)
    {
        string instName = context.JobDetail.Name;
        string instGroup = context.JobDetail.Group;

        JobDataMap dataMap = context.JobDetail.JobDataMap;

        string jobSays = dataMap.GetString("jobSays");
        float myFloatValue = dataMap.GetFloat("myFloatValue");
        ArrayList state = (ArrayList) dataMap["myStateData"];
        state.Add(DateTime.UtcNow);

        Console.WriteLine("Instance {0} of DumbJob says: {1}", instName, jobSays);
    }
}