• 定时执行任务


    首先  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重写

  • 相关阅读:
    【创建图像映射】
    【用图像代替文本作为超链接】
    【设置图像的提示文字】
    【设置图像的替换文字】
    【小玩意儿:在页面上设置链接发送E-mail】
    【锚点】
    【绝对路径/相对路径】
    【安装IDEA】
    【加法计算器】
    【Unlock xcode files?】
  • 原文地址:https://www.cnblogs.com/xuzai/p/4715275.html
Copyright © 2020-2023  润新知