網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、定時(shí)任務(wù)調(diào)度的方法或者組件:
任務(wù)定時(shí)器–FluentScheduler組件可以在C#和ASP.NET程序中使用,使用方法很簡(jiǎn)單,官方有使用案例:
FluentScheduler 中 對(duì)象: IJob(工作)、Registry(注冊(cè))、Schedule(計(jì)劃)
二、實(shí)例
項(xiàng)目需要一個(gè)按時(shí)執(zhí)行的任務(wù),每隔幾分鐘執(zhí)行一個(gè),或者每隔幾小時(shí)執(zhí)行一次等等,這個(gè)時(shí)候就需要一個(gè)定時(shí)的功能,最簡(jiǎn)單的就是用Timer自己寫一個(gè),但是自己寫的性能等各方面有可能不健全等等,而現(xiàn)在開源的庫(kù)也越來(lái)越多,功能也越來(lái)越好,直接拿來(lái)主義。
1.NuGet下載FluentScheduler控件
FluentScheduler定時(shí)任務(wù)庫(kù),通過(guò)nuget引用,可以設(shè)置各種事件間隔,,超級(jí)方便簡(jiǎn)單。
2.編寫一個(gè)注冊(cè)表。繼承Registry類
using FluentScheduler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NetFrameTest.test
{
public class MyRegistry : Registry
{
public MyRegistry()
{
// 每天執(zhí)行一次(這里是在每天的下午 15:40 分執(zhí)行),可以不用類,直接虛擬方法
Schedule(() => Console.WriteLine("It's 15:42 now.")).ToRunEvery(1).Days().At(15, 42);
// 每?jī)擅雸?zhí)行一次(指定一個(gè)時(shí)間間隔運(yùn)行,根據(jù)自己需求,可以是秒、分、時(shí)、天、月、年等。)
Schedule<MyJob>().ToRunNow().AndEvery(2).Seconds();
// 每五秒執(zhí)行一次(延遲一個(gè)指定時(shí)間間隔執(zhí)行一次計(jì)劃任務(wù))
Schedule<MyJob>().ToRunOnceIn(5).Seconds();
// 每月執(zhí)行一次(這里是在每月的第一周的周一3點(diǎn)執(zhí)行)
Schedule<MyJob>().ToRunNow().AndEvery(1).Months().OnTheLast(DayOfWeek.Friday).At(16, 0);
// 構(gòu)造函數(shù)執(zhí)行
Schedule(() => new MyOtherJob("Foo")).ToRunNow().AndEvery(2).Seconds();
// 先執(zhí)行第一個(gè)Job、再執(zhí)行第二個(gè)Job;完成后等5秒繼續(xù)循環(huán)
Schedule<MyJob>().AndThen<MyOtherJob>().ToRunNow().AndEvery(5).Minutes();
}
}
}
3.編寫定時(shí)執(zhí)行任務(wù),工作類
根據(jù)使用方法,是否實(shí)現(xiàn)IJob接口
public class MyJob : IJob
{
public void Execute()
{
Console.WriteLine($"MyJob 當(dāng)前時(shí)間:{DateTime.Now}");
}
}
public class MyOtherJob : IJob
{
private string Name;
public MyOtherJob(string name)
{
Name = name;
}
public void Execute()
{
Console.WriteLine($"MyOtherJob 姓名:{Name} 當(dāng)前時(shí)間:{DateTime.Now}");
}
}
4.初始化定時(shí)器
定時(shí)任務(wù)寫好之后只需要在Main中引用就可以了
// static void Main(string[] args)
JobManager.Initialize(new MyRegistry());
在ASP.NET程序的Global.asax文件中,首先初始化管理器,這樣定時(shí)器就開啟了。
protected void Application_Start()
{
JobManager.Initialize(new MyRegistry());
}
以上是第一種寫法,下面介紹第二種寫法,這里執(zhí)行多個(gè)任務(wù) 不同時(shí)間段
protected void Application_Start()
{
JobManager.AddJob<MyJob>(t=>t.ToRunEvery(1).Days().At(24,00));//每天的凌晨12點(diǎn) 執(zhí)行
JobManager.AddJob<MyOtherJob>(t => t.ToRunEvery(1).Months().OnTheLastDay().At(8,30));//每個(gè)月的最后一天早上八點(diǎn)半 執(zhí)行
JobManager.Start();//啟動(dòng)任務(wù)管理器
}
原文鏈接:https://www.cnblogs.com/springsnow/p/13158559.html
相關(guān)推薦
- 2022-11-25 Linux?Shell腳本多命令執(zhí)行邏輯的示例詳解_linux shell
- 2022-04-15 Python?yield?的使用淺析_python
- 2022-09-24 ASP.NET?MVC下拉框中顯示枚舉項(xiàng)_實(shí)用技巧
- 2022-06-06 uniApp實(shí)現(xiàn)滾動(dòng)視圖點(diǎn)擊錨點(diǎn)跳轉(zhuǎn)、點(diǎn)擊左側(cè)分欄時(shí)右側(cè)對(duì)應(yīng)內(nèi)容置頂、左右分欄聯(lián)動(dòng)、getSyste
- 2024-03-08 學(xué)習(xí)基于ssm框架前后端分離實(shí)現(xiàn)注冊(cè)登錄MD5加密的心得體會(huì)
- 2022-04-18 Taro 中的 用戶下拉事件,onPullDownRefresh
- 2022-07-02 react 替換頁(yè)面頭圖標(biāo)失敗
- 2022-04-11 python中pip安裝、升級(jí)以及升級(jí)固定的包_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支