Quartz.Net 任務計劃 Windows服務 分享用到的參考及遇到的問題 一、Quartz.Net 參考: 1、介紹及創建:https://www.cnblogs.com/best/p/7658573.html 2、MIsfire機制:https://www.jianshu.com/p/572 ...
- 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、問題:
- IWeeklyTrigger iwt = (IWeeklyTrigger)trigger;
- iwt.DaysOfWeek = (short)DaysOfWeek.Friday;// 0X20 ,不是5iwt.WeeksInterval = 2;
- _TASK_LOGON_TYPE.TASK_LOGON_GROUP:任務不執行;
- _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN:創建任務被拒絕,設計組策略設置。
- 關於腳本:參數接收及傳遞
.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服務