C# 超时工具类 第二版
附源码,没有附测试demo
之前的工具类:C# 给某个方法设定执行超时时间
/// <summary>
/// 超时工具
/// </summary>
public class TimeoutTools : IDisposable
{
private System.Windows.Forms.Timer timer; /// <summary>
/// 位置
/// </summary>
public uint Position { get; private set; } /// <summary>
/// 超时事件
/// </summary>
public event EventHandler TimeoutEvent; /// <summary>
/// Tick事件
/// </summary>
public event EventHandler TickEvent; /// <summary>
/// 步长值
/// <para>默认值1</para>
/// </summary>
public uint StepLength { get; set; } /// <summary>
/// 超时长度
/// <para>默认180</para>
/// </summary>
public uint TimeoutLength { get; set; } /// <summary>
/// 默认构造函数
/// </summary>
public TimeoutTools(System.ComponentModel.IContainer container)
{
this.StepLength = ;
this.TimeoutLength = ; this.timer = new System.Windows.Forms.Timer(container);
this.timer.Interval = ;
this.timer.Enabled = false; timer.Tick += (sender, e) =>
{
this.Position += this.StepLength; if (this.Position >= this.TimeoutLength)
{
this.Reset(); this.OnTimeOut();
}
else
{
if (this.TickEvent != null)
{
this.TickEvent(this, EventArgs.Empty);
}
}
};
} ///<summary>
/// 指定超时时间 执行某个方法
///</summary>
///<returns>执行 是否超时</returns>
public static bool DoAction(TimeSpan timeSpan, Action action)
{
if (action == null)
throw new ArgumentNullException("action is null"); bool timeout = true; try
{
// 异步调用Action
IAsyncResult result = action.BeginInvoke(null, null); // Wait
if (result.AsyncWaitHandle.WaitOne(timeSpan, false))
{
timeout = false;
} if (!timeout)
{
action.EndInvoke(result);
}
}
catch (Exception)
{
timeout = true;
} return timeout;
} /// <summary>
/// 设置计时器参数
/// </summary>
/// <param name="period">毫秒</param>
public void SetInterval(int period)
{
if (period == Timeout.Infinite)
{
this.timer.Enabled = false;
return;
}
this.timer.Interval = period;
this.timer.Enabled = true;
} /// <summary>
/// 接收到信号
/// </summary>
public void Reset()
{
this.Position = ;
} protected void OnTimeOut()
{
if (this.TimeoutEvent != null)
{
this.TimeoutEvent(this, EventArgs.Empty);
}
} public void Dispose()
{
if (this.timer == null)
return; this.timer.Enabled = false;
this.timer.Dispose();
}
}
C# 超时工具类 第二版的更多相关文章
- briup_jdbc自建工具类终极版
总结:此次构建工具类,难点在于查询,所需要的功能是 不管是 oracle还是mysql 都可以连接,并且 提供所需要的实体类,都可以将查询内容封装到实体类中去 遇到的难点 连接时,是从prppert ...
- 二维码生成工具类java版
注意:这里我不提供所需jar包的路径,我会把所有引用的jar包显示出来,大家自行Google package com.net.util; import java.awt.BasicStroke; im ...
- redis集群使用Java工具类(Java jedis集群工具类)
package com.xiaomi.weather.vote.webservices.util.redisCache; import com.google.common.base.Strings; ...
- java代码之美(12)---CollectionUtils工具类
java代码之美(12)---CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的, 而不是springframework下的CollectionUt ...
- java代码(12) ---CollectionUtils工具类
CollectionUtils工具类 CollectionUtils工具类是在apache下的,而不是springframework下的CollectionUtils 个人觉得在真实项目中Collec ...
- MySQL数据库工具类之——DataTable批量加入MySQL数据库(Net版)
MySQL数据库工具类之——DataTable批量加入数据库(Net版),MySqlDbHelper通用类希望能对大家有用,代码如下: using MySql.Data.MySqlClient; us ...
- Lucene第二篇【抽取工具类、索引库优化、分词器、高亮、摘要、排序、多条件搜索】
对Lucene代码优化 我们再次看回我们上一篇快速入门写过的代码,我来截取一些有代表性的: 以下代码在把数据填充到索引库,和从索引库查询数据的时候,都出现了.是重复代码! Directory dire ...
- 《python基础教程(第二版)》学习笔记 类和对象(第7章)
<python基础教程(第二版)>学习笔记 类和对象(第7章) 定义类class Person: def setName(self,name): self.name=n ...
- java邮件工具类【最终版】
http://www.xdemo.org/java-mail/ 对比链接中,添加了抄送和暗抄送功能(已解决,如图代码:抄送不能多个用户,会报错,未解之谜) sendHtmlmail方法可以发送附件以及 ...
随机推荐
- WebForms VS. MVC(翻译)
(本文翻译自CodeProject上阿三写的一篇文章,原文地址:http://www.codeproject.com/Articles/528117/WebForms-vs-MVC,讲了有关ASP.A ...
- Mac下配置Apache服务
这篇文章主要是针对Mac用户,第一次搭建本地开发环境的同学,已经搭建过的同学可以忽略. Mac自带的Apache还是XAMPP? That is a question. 其实自带的apache也够用了 ...
- 七天学会ASP.NET MVC (三)——ASP.Net MVC 数据处理
第三天我们将学习Asp.Net中数据处理功能,了解数据访问层,EF,以及EF中常用的代码实现方式,创建数据访问层和数据入口,处理Post数据,以及数据验证等功能. 系列文章 七天学会ASP.NET M ...
- OAuth2 Backend Web Application 验证过程
本文是从我的 github 博客转载的,原文请看. 一图胜千言.图片请自由转载,请保留图片的原始签名.
- 台式电脑、笔记本快捷选择Boot(启动项快捷键)大全
我们在安装系统时,会去设置电脑是从硬盘启动.U盘启动.光驱启动.网卡启动. 一般设置的方法有两种:一种是进BIOS主板菜单设置启动项顺序:另一种就是我在这里要介绍的快捷选择启动项. 以下是网友整理的各 ...
- Java异常内容总结
在程序开发中,可能存在各种错误,有些错误是可以避免的,而有些错误却是意想不到的,在Java中把这些可能发生的错误称为异常. Throwable类是所有异常类的超类,该类的两个直接子类是Error和Ex ...
- .Net中List<T> 泛型转成DataTable、DataSet
在开发过程过程中有时候需要将List<T>泛型转换成DataTable.DataSet,可以利用反射机制将DataTable的字段与自定义类型的公开属性互相赋值. 1.List<T& ...
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
- removeClass 按钮点击添加class效果
html代码: <div class="game"> <span class="active">全部</span> < ...
- MVC4做网站后台:用户管理 ——用户组补充
上次做完发现存在点问题. 1.是类型显示的为数字(如下图): 2.是不能根据类型进行查询. 首先做查询.easyui-datagrid可以添加toolbar. 在datagrid的data-optio ...