c# For web development, use the quartz timer for detailed explanation

一 prerequisite

(1) First need to refer to Common.Logging.dll and Quartz.dll.

(2) Secondly, we need to understand some basic knowledge

 Job

indicates a specific work to be performed, need to implement the IJob interface.

 JobDetail

JobDetail represents a specific executable scheduler, which points to Job.

 Trigger

represents the trigger of the work, when it is executed, how many times it is executed

 Scheduler

represents an execution plan, and multiple JobDetail and Trigger can be registered in one execution plan. When Trigger is combined with JobDetail, it can be dispatched by the Scheduler container.

二 Instance

(1) The following is a small example based on actual projects.

First, I am here to create a class that performs a concrete transaction. It needs to inherit IJob and implement a unique Execute.

public class SalaryJob: IJob
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
      Public void Execute(IJobExecutionContext context)
      {
          String args = "";
     
          Idictomes dc = new dictomes();
          dc.Insert(args);//call an interface


      }

    }

Second, we need to create a scheduler and a Trigger.

public class SalaryScheduler
    {
        Public static void Start()
        {
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();
        // string expression = "0 */1 * * * ?";//Execute every 5 seconds
            String expression = "0 57 23 L * ?";//23:57 on the last day of each month.
            IJobDetail job = JobBuilder.Create<SalaryJob>().Build();
            ITrigger trigger = TriggerBuilder.Create()
              .WithIdentity("triggerName", "groupName")
               .WithCronSchedule(expression)
               .Build();
     

            scheduler.ScheduleJob(job, trigger);
 
        
        
        }


    }

Note that the above salaryjob class is the implementation class for the specific implementation.

Then, call the start() method in Application_Start in the Global.asax file, because Application_Start is executed when the program is initialized. It is executed once in the life cycle of the web application. Here you can only put some common information, such as HttpApplicationState.

This will be executed according to the set timer after running.