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中的配置信息 ...
随机推荐
- 剑指offer-36:数组中的逆序对
参考:1. https://www.geeksforgeeks.org/merge-sort/ 2.<剑指Offer:名企面试官精讲典型编程题> 题目描述 在数组中的两个数字,如果前面一个 ...
- Eclipse——关联源代码
Eclipse——关联源代码 摘要:本文主要说明了如何在Eclipse里关联源代码. 下载源码包 首先去想要关联的jar包的官网下载对应jar包的源代码,拿Tomcat的类库举例,先去官网下载源码包: ...
- 魔兽争霸RPG地图开发速成教程
魔兽争霸RPG地图开发速成教程 1 打开WE编辑器 下载地址 http://rpg.dz.blizzard.cn/authors-home/editor-download 然后新建地图 2 打开工 ...
- C语言程序设计100例之(11):求质数
例11 求质数 问题描述 质数是指除了有1和自身作为约数外,不再有其他约数的数.比如:3.5.7是质数.而9不是质数,因为它还有约数3. 编写程序求给定区间中的所有质数. 输入格式 两个整数a和b, ...
- Password Management:Hardcoded Password 密码管理:硬编码密码
- C# 使用 csc.exe 实现命令行生成
概述 CSC是什么呢?CSC就是 C-Sharp Compiler (中文就是C#编译器),作用是把我们的 cs 源文件变异成dll 或者是exe , 一般安装完VS 后,就会有这个文件: 这里 ...
- 【Beta阶段】第十二周Scrum会议
[Beta阶段]第十二周Scrum会议 本次会议为第十二周第一次Scrum Meeting,会议对Beta阶段工作进行了总结,针对Beta阶段还未完成的问题进行了讨论. 会议时间为2019.12.3. ...
- python捕捉详细异常堆栈的方法
python中有 try——except 的方法捕获异常,可以获取到异常的种类以及自定义异常, 但是有时候对于debug测试来说,信息不全,比如说 触发异常的具体位置在哪: import traceb ...
- Redis基础类型常用操作命令
Redis基础类型常用操作命令 概念:Redis是用C语言开发的一个开源的高性能键值对数据库. 特征: 数据间没有必然的联系 内部采用单线程机制进行工作 高性能 多数据类型支持 字符串类型 Strin ...
- (day65、66)Vue基础、指令、实例成员、JS函数this补充、冒泡排序
目录 一.Vue基础 (一)什么是Vue (二)为什么学习Vue (三)如何使用Vue 二.Vue指令 (一)文本指令 (二)事件指令v-on (三)属性指令v-bind (四)表单指令v-model ...