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方法可以发送附件以及 ...
随机推荐
- 微信支付开发demo
微信支付的时候,发红包部分的demo,在curl部分还要加上 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true); curl_setopt($ch,CURLOPT ...
- Lesson 3 Please send me a card
Text Postcards always spoil my holidays. Last summer, I went to Italy. I visited museums and sat in ...
- Hadoop学习笔记—14.ZooKeeper环境搭建
从字面上来看,ZooKeeper表示动物园管理员,这是一个十分奇妙的名字,我们又想起了Hadoop生态系统中,许多项目的Logo都采用了动物,比如Hadoop采用了大象的形象,所以我们可以猜测ZooK ...
- 【Java并发编程实战】-----“J.U.C”:ReentrantLock之一简介
注:由于要介绍ReentrantLock的东西太多了,免得各位客官看累,所以分三篇博客来阐述.本篇博客介绍ReentrantLock基本内容,后两篇博客从源码级别分别阐述ReentrantLock的l ...
- 你可能不知道的7个CSS单位
如果你是一名前端开发工程师,一般px和em使用频率比较高.但是今天本文的重点是介绍一些我们使用很少.甚至么有听说的单位. 一.重温em <style type="text/css&qu ...
- 我也来说说DDD~大话目录
回到占占推荐博客索引 DDD之前没有接触过,但一但有了接触就一发不可收拾,他会带去进入一个全新的世界! DDD不是新技术,而是新思想,新模式,是软件开发领域的一次突破,它更接近于业务,对于业务的改动它 ...
- ASP.net 常用服务器控件
新人初学,有错请指,大神轻喷. .net中有HTML控件和标准服务器控件. 老师教学用的是vs2010是这样分的,不知道15里是不是这样. 如果使用HTML控件我们就会发现页面中加载了原本HTML代码 ...
- Oracle 创建普通用户,并赋予权限
采用sys or system / manager as sysdba; 连接数据库. 创建普通用户konglin: create user konglin identified by pwd_ora ...
- maven的聚合与继承
新建一个空的maven项目user-parent Pom.xml内容 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
- 修改input框默认黄色背景
input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill { -webkit-box-shadow: 0 0 ...