C#&.Net干货分享-构建后台自动定时任务的源码
1、创建一个自动处理中心任务参数的类,直接源码:
namespace Frame.AutoProcess
{
/// <summary>
/// 委托(用于异步处理任务)
/// </summary>
/// <param name="parameter">任务参数</param>
/// <returns>是否执行成功</returns>
public delegate bool TaskHandle(object parameter);
/// <summary>
/// 自动处理中心任务参数
/// </summary>
public class BackgroundTask
{
#region 私有成员
private bool _IsOnce = true; //是否执行一次
private bool _IsExecuteNow = false; //是否立即执行,对于重复执行生效
private int _Interval = 86400; //重复执行时的执行间隔,秒为单位,默认为一天,如果只执行一次并且想马上执行将该值设置为0
private bool _IsAbortExcute = false; //终止执行(设置为true时,系统将不会执行Timer的事件)
private TaskHandle _ExecuteMethod = null; //任务执行方法
private object _Parameter = null; //任务执行方法参数
private DateTime? _ExecuteDateTime = null;
#endregion
#region 属性
/// <summary>
/// 是否已经终止
/// </summary>
public bool IsAbortExcute
{
get { return this._IsAbortExcute; }
}
/// <summary>
/// 执行的方法
/// </summary>
public TaskHandle ExecuteMethod
{
get { return this._ExecuteMethod; }
}
#endregion
#region 构造函数
/// <summary>
/// 任务参数构造函数
/// </summary>
/// <param name="executeMethod">执行方法</param>
/// <param name="parameter">方法参数</param>
/// <param name="isOnce">是否执行一次</param>
/// <param name="interval">执行间隔(秒),默认为24小时</param>
/// <param name="isExecuteNow">是否立即执行</param>
/// <param name="executeDateTime"></param>
public BackgroundTask(TaskHandle executeMethod, object parameter = null, bool isOnce = true, int interval = 86400, bool isExecuteNow = false, DateTime? executeDateTime = null)
{
this._ExecuteMethod = executeMethod;
this._Parameter = parameter;
this._IsOnce = isOnce;
this._Interval = interval;
if (interval < 0)
{
this._Interval = 1;
}
this._IsExecuteNow = isExecuteNow;
this._ExecuteDateTime = executeDateTime;
}
#endregion
/// <summary>
/// 开始执行任务
/// </summary>
/// <returns></returns>
public void Execute()
{
if (!AutoProcessTask.IsStart) return;
int interval = _Interval * 1000;
if (interval == 0) interval = 1;
/*
Timer是提供以指定的时间间隔执行某方法的这样一种机制,
* 即如果想要实现一个定时发送数据,比如每隔3s中发送一次心跳报文,或者执行某个指定的方法,
* 都可以考虑用Timer类来实现,
* 不过要提出的是Timer类一边用来做一些比较简单又不耗时间的操作。
* 据说是因为它执行的任务仍然在主线程里面
*/
if (this._ExecuteDateTime.HasValue) //按执行时间时每秒跑一次
interval = 1000;
Timer _timer = new Timer(interval);
_timer.AutoReset = !_IsOnce;
_timer.Enabled = true;
if (_IsExecuteNow && !_IsOnce) //立即执行
{
_ExecuteMethod.BeginInvoke(_Parameter, null, null);
}
_timer.Elapsed += new ElapsedEventHandler(Start);
if (_IsOnce && _timer != null)
{
_timer.Enabled = false;
_timer.Elapsed -= new ElapsedEventHandler(Start);
_timer = null;
}
}
/// <summary>
/// 开始执行Timer具体方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Start(object sender, ElapsedEventArgs e)
{
if (this._ExecuteDateTime.HasValue)
{
DateTime now = DateTime.Now;
DateTime executeTime = this._ExecuteDateTime.Value;
if (executeTime.Hour == now.Hour && executeTime.Minute == now.Minute && executeTime.Second == now.Second)
{
_ExecuteMethod.BeginInvoke(_Parameter, null, null);
}
else
{
(sender as Timer).Interval = 1000;
}
}
else
{
_ExecuteMethod.BeginInvoke(_Parameter, null, null);
}
}
}
}
2、定义自动处理任务的任务操作类,直接源码:
namespace Frame.AutoProcess
{
/// <summary>
/// 自动处理任务
/// </summary>
public class AutoProcessTask
{
/// <summary>
/// 执行Execute方法后事件
/// </summary>
public static event EventHandler EventAfterExecute;
/// <summary>
/// 是否已经开始运行
/// </summary>
private static bool isStart = false;
/// <summary>
/// 任务列表
/// </summary>
private static List<BackgroundTask> taskList = new List<BackgroundTask>();
/// <summary>
/// 是否启动
/// </summary>
public static bool IsStart
{
get { return isStart; }
}
/// <summary>
/// 添加任务
/// </summary>
/// <param name="task">任务对象</param>
public static void AddTask(BackgroundTask task)
{
if (task != null && isStart)
{
BackgroundTask tempTask = taskList.Where(O => O.ExecuteMethod == task.ExecuteMethod).FirstOrDefault();
if (tempTask != null) //系统已存在该任务
{
return;
}
taskList.Add(task); //添加到任务列表
task.Execute(); //开始执行任务
}
}
/// <summary>
/// 执行任务
/// </summary>
public static void Execute()
{
isStart = true;
if (EventAfterExecute != null)
{
EventAfterExecute(null, null);
}
}
}
}
3、调用方式,一般都是在工程启动的时候添加如下直接源码:
BackgroundTask extractAttachmentTextTask = new BackgroundTask((args) =>
{
string errMsg = string.Empty;
try
{
你的逻辑。。。。。。。。。。
}
catch
{ }
return true;
}, null, false, 3600, false);//每小时执行一次
C#&.Net干货分享-构建后台自动定时任务的源码的更多相关文章
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(2)-easyui构建前端页面框架[附源码]
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(2)-easyui构建前端页面框架[附源码] 开始,我们有了一系列的解决方案,我们将动手搭建新系统吧. 用 ...
- NhibernateProfiler-写个自动破解工具(源码)
04 2013 档案 [屌丝的逆袭系列]是个人都能破解之终结NhibernateProfiler-写个自动破解工具(源码) 摘要: 破解思路分析及手动破解 增加“附加到进程”功能--功能介绍增加“ ...
- Delphi制作QQ自动登录器源码
Delphi制作QQ自动登录器源码 http://www.cnblogs.com/sunsoft/archive/2011/02/25/1964967.html 以TM2009为例,检查了一下,未登 ...
- C#&.Net干货分享- 构建Spire-Office相关Helper操作Word、Excel、PDF等
先下载好如下的组件: 直接使用完整源码分享: namespace Frame.Office{ /// <summary> /// Spire_WordHelper /// ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(2)-easyui构建前端页面框架[附源码]
系列目录 前言 为了符合后面更新后的重构系统,本文于2016-10-31日修正一些截图,文字 我们有了一系列的解决方案,我们将动手搭建新系统吧. 后台系统没有多大的UI视觉,这次我们采用的是标准的左右 ...
- 分享非常漂亮的WPF界面框架源码及插件化实现原理
在上文<分享一个非常漂亮的WPF界面框架>中我简单的介绍了一个界面框架,有朋友已经指出了,这个界面框架是基于ModernUI来实现的,在该文我将分享所有的源码,并详细描述如何基于Mod ...
- extjs+MVC4+PetaPoco+AutoFac+AutoMapper后台管理系统(附源码)
前言 本项目使用的开发环境及技术列举如下:1.开发环境IDE:VS2010+MVC4数据库:SQLServer20082.技术前端:Extjs后端:(1).数据持久层:轻量级ORM框架PetaPoco ...
- android完整智能家居、备忘录、蓝牙配对、3D动画库、购物车页面、版本更新自动安装等源码
Android精选源码 app 版本更新.下载完毕自动自动安装 android指针式分数仪表盘 ANdroid蓝牙设备搜索.配对 Android 图片水印框架,支持隐形数字水印 android3D旋转 ...
- 使用Jenkins+Pipline 持构建自动化部署之安卓源码打包、测试、邮件通知
一.引言 Jenkins 2.x的精髓是Pipeline as Code,那为什么要用Pipeline呢?jenkins1.0也能实现自动化构建,但Pipeline能够将以前project中的配置信息 ...
随机推荐
- 参加杭州 2019 AI Bootcamp有感与总结(2)
接上篇 参加杭州 2019 AI Bootcamp有感与总结(1) - repeatedly - 博客园 午餐畅谈的收获 先感谢主办方提供的午餐,中午午休的时候,大家聊了很多,或者说主要是听大佬谈.聊 ...
- 最新IDEA永久激活
此教程已支持最新2019.2版本 本教程适用Windows.Mac.Ubuntu等所有平台. 激活前准备工作 配置文件修改已经不在bin目录下直接修改,而是通过Idea修改 如果输入code一直弹出来 ...
- SpringCloud 亿级流量 架构演进
疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] 前言 Crazy ...
- jQuery实现回车触发登录按钮的功能
jQuery实现回车触发登录按钮的功能,代码如下: $('body').keyup(function(e){ if(e.keyCode===13){ $('.login').click() } }) ...
- 利用文件保存 Map 健值信息
Map<String,MobileCard> cards = new HashMap<String,MobileCard>(); Map<String,List<C ...
- C lang:Protect array data——Const
Xx_Introduction Use pointer translate parameter array original data will change data,and use const p ...
- cesium 雷达扫描(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- 腾讯 Techo 开发者大会首发来袭!云原生中间件技术实践等你来!
腾讯 Techo 开发者大会是由腾讯云发起的面向全球开发者和技术爱好者的年度盛会,2019 年 11 月 6 日 - 7 日将在北京嘉里大酒店首次召开. 作为一个专注于前沿技术研讨的非商业大会,Tec ...
- TICK技术栈(五)Kapacitor安装及使用
1.什么是Kapacitor? Kapacitor是InfluxData开源的数据处理引擎.它可以处理来自InfluxDB的流数据和批处理数据,并且用户可以用tickScript脚本来处理,监视和警报 ...
- mysql在本地已经启动,但是在网页上不能直接访问的解决
1.将mysql文件下的my.ini中的路径代码 # 设置mysql的安装目录 basedir=E:/Develop/mysql # 设置mysql数据库的数据的存放目录 datadir=E:/Dev ...