码迷,mamicode.com
首页 > 其他好文 > 详细

定时执行任务

时间:2015-08-09 16:48:21      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

首先  Install-Package Quartz;

技术分享
using System;
using Quartz;
using System.Collections.Specialized;
using Quartz.Impl;
using Common.Logging;

namespace LisWms.Sch
{
    public class Scheduler
    {
        private static Scheduler instance;

        private static Object locker = new Object();

        public static IScheduler scheduler;

        /// <summary>
        /// 创建Scheduler
        /// </summary>
        /// <returns></returns>
        public static Scheduler GetInstance()
        {
            lock (locker)
            {
                if (instance == null)
                {
                    instance = new Scheduler();
                }
            }
            return instance;
        }

        private Scheduler()
        {
            //工厂方式创建 IScheduler接口
            ISchedulerFactory sf = new StdSchedulerFactory();
            scheduler = sf.GetScheduler();
        }

        public static void Start()
        {
            scheduler.Start();
        }
        /// <summary>
        /// 
        /// </summary>
        public static void Stop()
        {
            scheduler.Shutdown(true);
            instance = null;
        }
        /// <summary>
        /// 暂停所有动做
        /// </summary>
        public static void Pause()
        {
            scheduler.PauseAll();
        }
        /// <summary>
        /// 恢复所有动做
        /// </summary>
        public static void Resume()
        {
            scheduler.ResumeAll();
        }
        /// <summary>
        /// 添加多个job任务
        /// </summary>
        /// <param name="jobDetail"></param>
        /// <param name="trigger"></param>
        public static void AddJob(IJobDetail jobDetail, ITrigger trigger)
        {
            // 当Trigger认为应该触发的时候就会调用(实际上是Scheduler调用)job.execute方法了。    
       
            scheduler.ScheduleJob(jobDetail, trigger);
          
        }
        /// <summary>
        /// 删除相关的job任务
        /// </summary>
        /// <param name="jobKey"></param>
        /// <returns></returns>
        public static bool DeleteJob(JobKey jobKey)
        {
            return scheduler.DeleteJob(jobKey);
        }

    }
}
View Code

如上 Scheduler 类

设置 定时调度任务

技术分享
 //初始化

            Scheduler.GetInstance();
          
            #region AppUserJob
            //==========AppUserJob===========

            IJobDetail AppUserJob = JobBuilder.Create<AppUserJob>()  //创建一个作业
                .WithIdentity("AppUserJob", "lisWmsSyn")
                .Build();

            ITrigger AppUserJobTri = TriggerBuilder.Create()
                                        .WithIdentity("AppUserJobTri", "lisWmsSyn")
                                        .StartNow()
                                        .WithSimpleSchedule(x => x         //触发时间,60秒一次。
                                            .WithIntervalInSeconds(60)
                                            .WithMisfireHandlingInstructionFireNow()
                                            .RepeatForever())              //不间断重复执行
                                        .Build();
            //帮助类
            HelpEvent help = new HelpEvent();
            help.SetMsgEvent -= new HelpEvent.SetMsg(AddMsg);
            help.SetMsgEvent += new HelpEvent.SetMsg(AddMsg);
            Scheduler.scheduler.Context.Put("HelpEvent", help);



            Scheduler.AddJob(AppUserJob, AppUserJobTri);      //把作业,触发器加入调度器。
            #endregion
View Code

所有的任务类都要继承 IJob接口 不要对 Execute重写

定时执行任务

标签:

原文地址:http://www.cnblogs.com/xuzai/p/4715275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!