定时任务笔记

编程

  •  Quartz.Net
  •  任务计划
  • Windows服务

分享用到的参考及遇到的问题

一、Quartz.Net

参考:

1、介绍及创建:https://www.cnblogs.com/best/p/7658573.html

2、MIsfire机制:https://www.jianshu.com/p/572322b36383

3、代码:

public class QuartzManager

{

private static IScheduler sched = null;

 

static QuartzManager()

{

ISchedulerFactory sf = new StdSchedulerFactory();

sched = sf.GetScheduler();

sched.Start();

}

public static void AddJob<T>(string JobName, string CronTime, string jobData) where T : IJob

{

IJobDetail jobCheck = JobBuilder.Create<T>().WithIdentity(JobName, JobName + "_Group").UsingJobData("jobData", jobData).Build();

CronScheduleBuilder csb = CronScheduleBuilder.CronSchedule(CronTime);

csb.WithMisfireHandlingInstructionFireAndProceed();

 

ITrigger trigger = TriggerBuilder.Create()

.WithIdentity(JobName + "_Trigger", JobName + "_TriggerGroup")

//设置激活失败处理机制

.WithCronSchedule(CronTime, a => a.WithMisfireHandlingInstructionFireAndProceed())

.ForJob(JobName, JobName + "_Group")

.Build();

sched.ScheduleJob(jobCheck, trigger);

}

public static void DeleteJob(string JobName)

{

JobKey jk = new JobKey(JobName, JobName + "_Group");

sched.DeleteJob(jk);

}

}

4、IIS回收

参考:https://www.cnblogs.com/xielong/p/6802329.html

5、遇到的问题:iis重启,job没有执行

二、任务计划

1、查看本机任务计划https://jingyan.baidu.com/article/b87fe19e53b2505218356896.html

2、介绍及创建:https://www.cnblogs.com/tonge/p/4410066.html

3、代码:http://note.youdao.com/noteshare?id=92016986d3da885bc2f8ca3c32ed0efd

4、问题:

  1. IWeeklyTrigger iwt = (IWeeklyTrigger)trigger;
  2. iwt.DaysOfWeek = (short)DaysOfWeek.Friday;// 0X20 ,不是5iwt.WeeksInterval = 2;
  3. _TASK_LOGON_TYPE.TASK_LOGON_GROUP:任务不执行;
  4. _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN:创建任务被拒绝,设计组策略设置。
  5. 关于脚本:参数接收及传递

.vbs:

set ws=WScript.CreateObject("WScript.Shell")

ws.Run ""+WScript.Arguments(0)+" " & WScript.Arguments(1) & " " & WScript.Arguments(2) & " " & WScript.Arguments(3),0

.bat:

@ECHO OFF

if exist %1 (

Forfiles /p %1 /s /d -%2 /m %3 /c "cmd /c del /q /f @path"

)

三、Windows服务

1、查看本机服务:https://jingyan.baidu.com/article/19020a0a772a5c529d28420c.html

2、介绍及创建:https://www.cnblogs.com/alala666888/p/3421492.html

3、结合redis发布订阅、System.Threading.Timer使用:

http://note.youdao.com/noteshare?id=525c579ae3129fd7eda29c4773c3652f

四、小结:定时任务与程序分开较好处理,结合项目选用任务计划或者windows服务

 

以上是 定时任务笔记 的全部内容, 来源链接: utcz.com/z/515581.html

回到顶部