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中的配置信息 ...
随机推荐
- Linux中环境变量相关文件的区别
Linux下各种不同环境变量相关文件的作用: 1. /etc/environment 设置整个系统的环境,系统启动时,该文件被执行. 2. /etc/profile 设置所有用户的环境,当用 ...
- Leetcode7 : Reverse Integer 整数反转问题
问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...
- Python中的四种交换数值的方法
交换两个变量的值方法,这个面试题如果只写一种当然很简单,没什么可以说的. 今天这个面试是问大家有几种办法来实现交换两个变量的值. 在没开始看具体答案前,你可以先想想看 下面分别来说说这几种方法 方法一 ...
- PHP实现微信扫码自动登陆与注册,参考实例
微信开发已经是现在phper必须要掌握的一项基本的技术了,其实做过微信开发的都知道微信接口非常的强大做起来也非常的简单,这里我们一起来看一个微信自动登陆注册的例子. php 微信扫码 pc端自动登陆注 ...
- SpringBoot系列-整合Mybatis(注解方式)
目录 一.常用注解说明 二.实战 三.测试 四.注意事项 上一篇文章<SpringBoot系列-整合Mybatis(XML配置方式)>介绍了XML配置方式整合的过程,本文介绍下Spring ...
- docker升级步骤及注意事项
centos系统默认安装的docker版本是1.13版本,在安装部分镜像时可能出现兼容问题,本文通过实际操作总结Docker升级最新版本步骤及可能出现的问题,供各位参考. 环境:CentOS Linu ...
- RAC环境下修改字符集
跟单实例多少有点区别ORACLE 11g RAC 两节点第一步 查看字符集PRIMARY-SYS@mydb2>select userenv('language') from dual; USER ...
- 运维工程师必会工具(Nmap和TCPdump)
1.NMap工具 主要功能:探测主机是否在线.扫描主机开放端口和嗅探网络服务,用于网络探测和安全扫描. NMap支持很多扫描技术,例如:UDP.TCPconnect().TCPSYN(半开扫描).ft ...
- ASCII码表收藏
ASCII码表 ASCII码值 ESC键 VK_ESCAPE (27)回车键: VK_RETURN (13)TAB键: VK_TAB (9)Caps Lock键: VK_CAPITAL (20)Shi ...
- Httpclient4.5.*HttpClient请求,对于新建httpclient实例时保持会话
package net.bill99.httpconsel; import java.io.IOException; import java.util.*; import java.util.Map. ...