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方法可以发送附件以及 ...
随机推荐
- SQL Server 事件通知(Event notifications)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 基础知识(Rudimentary Knowledge) 事件通知监控DDL(NotifyQue ...
- Sql Server 2012新特性 Online添加非空栏位.
我们都知道,Sql Server在一个数据量巨大的表中添加一个非空栏位是比较费心的,缺乏经验的DBA或是开发人员甚至可能鲁莽地直接添加导致阻塞相应业务,甚至可能因为资源欠缺造成实例的全局问题.当然这都 ...
- 用rem实现WebApp自适应的优劣分析
关于rem实现屏幕自适应布局的讨论还是比较多的,刚好我也看到使用rem实现自适应的web app,所以也来凑下热闹. 说起rem,免不了要联系到em.px,这里简单提提他们的定义和特点. 1. px: ...
- 新浪计数业务之Redis
今天听一个同事说新浪使用的是Redis,于是自己将研究的过程整理出来以备后用. 我们都知道微博这玩意儿现在很火,新浪作为国内最早使用redis,并且是国内最大的redis使用者,当然备受人们关注.新浪 ...
- js动态生成选项之考试系统(一)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- Lua table之弱引用
Lua采用了基于垃圾收集的内存管理机制,因此对于程序员来说,在很多时候内存问题都将不再困扰他们.然而任何垃圾收集器都不是万能的,在有些特殊情况下,垃圾收集器是无法准确的判断是否应该将当前对象清理.这样 ...
- Spring学习记录(九)---通过工厂方法配置bean
1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. package com.guigu.spring.factory; public class C ...
- Distribution1:Distribution Reader
在transactional replication中,在publication中执行了一个更新,例如:update table set col=? Where ?,如果table中含有大量的数据行, ...
- 轻量级前端MVVM框架avalon - 执行流程1
基本上确定了avalon的几个重要元素的关系: M,即model,一个普通的JS对象,可能是后台传过来的,也可能是直接从VM中拿到,即VM.$json.有关的这个$json的名字还在商讨 V,即Vie ...
- JavaScript变量声明提前
上周四吃完午饭,leader发了一道JavaScript的题目给我们做,我们Team里面有做前端的,有做后台的,也有做mobile web的,所以大家对题目的理解各自都不一样,然后在QQ讨论组里面进行 ...