定时任务FluentScheduler
1、Nuget 安装包

2、创建3个不同的任务
public class MyJob : IJob
{
void IJob.Execute()
{
Trace.WriteLine("现在时间是:" + DateTime.Now);
}
}
public class MyOtherJob : IJob
{
void IJob.Execute()
{
Trace.WriteLine("这是另一个 Job ,现在时间是:" + DateTime.Now);
}
}
public class MyComplexJob : IJob
{
void IJob.Execute()
{
Trace.WriteLine("这是比较复杂的 Job ,现在时间是:" + DateTime.Now);
}
}
3、添加任务注册类,用于在Global中注册
public class Demo : Registry
{
public Demo()
{
//利用反射获取所有的任务来执行
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IJob)))).ToArray();
foreach (Type type in types)
{
//一个任务一个线程
Task.Factory.StartNew(() =>
{
IJob ss = (IJob)Activator.CreateInstance(type);
Schedule(() =>
{
ss.Execute();
}).ToRunNow().AndEvery().Seconds();
});
} //// Schedule an IJob to run at an interval
//// 立即执行每两秒一次的计划任务。(指定一个时间间隔运行,根据自己需求,可以是秒、分、时、天、月、年等。)
//Schedule<MyJob>().ToRunNow().AndEvery(2).Seconds(); //// Schedule an IJob to run once, delayed by a specific time interval
//// 延迟一个指定时间间隔执行一次计划任务。(当然,这个间隔依然可以是秒、分、时、天、月、年等。)
//Schedule<MyJob>().ToRunOnceIn(5).Seconds(); //// Schedule a simple job to run at a specific time
//// 在一个指定时间执行计划任务(最常用。这里是在每天的下午 1:10 分执行)
//Schedule(() => Trace.WriteLine("It's 1:10 PM now.")).ToRunEvery(1).Days().At(13, 10); //Schedule(() =>
//{ // // 做你想做的事儿。
// Trace.WriteLine("It's 1:10 PM now."); //}).ToRunEvery(1).Days().At(13, 10); //// Schedule a more complex action to run immediately and on an monthly interval
//// 立即执行一个在每月的星期一 3:00 的计划任务(可以看出来这个一个比较复杂点的时间,它意思是它也能做到!)
//Schedule<MyComplexJob>().ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0); //// Schedule multiple jobs to be run in a single schedule
//// 在同一个计划中执行两个(多个)任务
//Schedule<MyJob>().AndThen<MyOtherJob>().ToRunNow().AndEvery(5).Minutes(); }
}
4、在 Global.asax 中注册
JobManager.Initialize(new Demo());
其它:
也可以手动来进行启动、停止
/// <summary>
/// 启动定时任务
/// </summary>
public static void StartUp()
{
JobManager.Initialize(new Demo());
} /// <summary>
/// 停止定时任务
/// </summary>
public static void Stop()
{
JobManager.Stop();
}
扩展:自定义时间及是否启用
public class BaseJob
{
public int Second { get; set; } public bool Enabled { get; set; } public virtual void Execute() {
}
}
public class MyJob : BaseJob
{
public MyJob() {
Second = ;
Enabled = true;
}
public override void Execute()
{
Trace.WriteLine("MyJob 现在时间是:" + DateTime.Now);
}
}
public class MyOtherJob : BaseJob
{
public MyOtherJob()
{
Second = ;
Enabled = true;
}
public override void Execute()
{
Trace.WriteLine("MyOtherJob 这是另一个 Job ,现在时间是:" + DateTime.Now);
}
}
public Demo()
{
//利用反射获取所有的任务来执行
//var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IJob)))).ToArray();
var types = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type=>type.BaseType == typeof(BaseJob));
foreach (Type type in types)
{
BaseJob ss = (BaseJob)Activator.CreateInstance(type);
if (ss.Enabled)
{
//一个任务一个线程
Task.Factory.StartNew(() =>
{
Schedule(() =>
{
ss.Execute();
}).ToRunNow().AndEvery(ss.Second).Seconds();
});
}
}
}
定时任务FluentScheduler的更多相关文章
- 定时任务FluentScheduler 学习笔记 .net
第一步添加引用 GitHub源码地址 与详细用法 https://github.com/fluentscheduler/FluentScheduler 下面开始简单的实现 /// <summar ...
- 优秀 .NET 开源项目集锦
Github 地址: https://github.com/jasonhua95/awesome-dotnet-core awesome-dotnet-core .NET Core框架.库和软件的中文 ...
- C#定时任务组件之FluentScheduler
FluentScheduler是.NET开源处理定时任务组件 1.任务的创建注册 public static void TaskActionByMinutes(Action action, int c ...
- .NET定时任务执行管理器开源组件–FluentScheduler
在日常项目里通常会遇到定时执行任务的需求,也就是定时器..NET Framework里关于定时器的类有3个,分别是System.Windows.Forms.Timer.System.Timers.Ti ...
- [转]推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler
在C#WINFORM或者是ASP.NET的WEB应用程序中,根据各种定时任务的需求,比如:每天的数据统计,每小时刷新系统缓存等等,这个时候我们得应用到定时器这个东东. .NET Framework有自 ...
- 推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler定时器
在C#WINFORM或者是ASP.NET的WEB应用程序中,根据各种定时任务的需求,比如:每天的数据统计,每小时刷新系统缓存等等,这个时候我们得应用到定时器这个东东. .NET Framework有自 ...
- ASP.NET MVC 使用FluentScheduler做定时任务
源代码地址: https://github.com/fluentscheduler/FluentScheduler 使用NuGet安装FluentScheduler 这是我实际项目中用到的代码,也可看 ...
- C#定时任务之FluentScheduler
一.业务需求 平台首页,有几个指标统计,产品不要求实时性,觉得一天更新一次可以接受. 最后决定用FluentScheduler定时执行统计,redis缓存结果. 每天晚上1点进行定时任务统计,将统计结 ...
- C#/ASP.NET定时任务执行管理器组件–FluentScheduler定时器
必须JobManager初始化 方式1: public void Start() { JobManager.AddJob(() => FetchingDa ...
随机推荐
- log4j.xml简单配置实现在控制台打印sql执行语句【加注释】
转: log4j.xml简单配置实现在控制台打印sql执行语句 2017年09月27日 13:02:34 艾然丶 阅读数 8804 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协 ...
- EasyUI下拉框级联
EasyUI用来实现后台界面还是可以的,毕竟面对的是小众群体而非广大的用户,简单为美.这里想聊的功能是一种下拉框的联动,比如我选中了下拉框A的某一项,那么下拉框B的选项就是甲乙丙丁,如果我选了A的另一 ...
- webpack——Modules && Hot Module Replacement
blog:JavaScript Module Systems Showdown: CommonJS vs AMD vs ES2015 官网链接: Modules 官网链接:Hot Module Rep ...
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_04-用户认证技术方案-SpringSecurityOauth2
2.3 Spring security Oauth2认证解决方案 本项目采用 Spring security + Oauth2完成用户认证及用户授权,Spring security 是一个强大的和高度 ...
- 123457123457#0#-----com.yuming.TruckCarRun01--前拼后广--大卡车游戏cym
com.yuming.TruckCarRun01--前拼后广--大卡车游戏cym
- 123457123457#0#-----com.threeapp.PaoPaoLong01-----泡泡龙大作战01
com.threeapp.PaoPaoLong01-----泡泡龙大作战01
- Yarn简单介绍及内存配置
本文出自:http://blog.chinaunix.net/uid/28311809/abstract/1.html 在这篇博客中,主要介绍了Yarn对MRv1的改进,以及Yarn简单的内存配置和Y ...
- hive-1.1.0-cdh5.11.1-src compile
1. download cdh hive src http://archive.cloudera.com/cdh5/cdh/5/hive-1.1.0-cdh5.11.1-src.tar.gz 2. ...
- LNMP一键环境安装多PHP版本共存的方法
多PHP版本只支持LNMP模式,LNMPA.LAMP模式下不支持!要使用多PHP先安装多PHP版本,在lnmp1.4源码(lnmp1.3的不行哦)目录下运行:./install.sh mphp 按提示 ...
- mysl创建用户+授权+增、删、改查
1.mysql的root用户无法给普通用户授权问题处理 update mysql.user set Grant_priv='Y' where User='root' and Host='%': flu ...