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方法可以发送附件以及 ...
随机推荐
- SqlServer英文单词全字匹配
环境:Vs2013+Sql Server2012 问题:现在数据库记录如下: Sentence列保存的是英文的句子,我现在想找出所有包含“I”(单词)的句子,如果我用 Sentence like '% ...
- Oracle编程脚本记录
--命令窗口查询 exec 存储名.包名.视图; select 函数名 from dual; create or replace procedure PR_test is begin --存储过程的代 ...
- 2013 duilib入门简明教程 -- XML配置界面(6)
前面那些教程都是为了让小伙伴们从win32.MFC过渡到duilib,让大家觉得duilib不是那么陌生,如果大家现在还对duilib非常陌生的话,那就说明前面的教程做得不好,请大家在下面留言 ...
- Oracle 超长字符串分割劈分
Oracle 超长字符串分割劈分,具体能有多长没测过,反正很大.... 下面,,,,直奔主题了: CREATE OR REPLACE FUNCTION splitstr(p_string IN clo ...
- salesforce 零基础学习(四十五)Approval Lock & UnLock相关注意事项
我们都知道,当一条记录进入审批流程以后会自动加锁,apex提供Approval类的lock和unlock方法可以让我们使用代码对记录进行加锁和解锁. 项目中遇到一个需求,需要当某种情况下对记录进行先解 ...
- iOS-----Crash文件分析(一)
开发程序的过程中不管我们已经如何小心,总是会在不经意间遇到程序闪退.脑补一下当你在一群人面前自信的拿着你的App做功能预演的时候,流畅的操作被无情地Crash打断.联想起老罗在发布Smartisan ...
- Windows下安装python2和python3双版本
现在大家常用的桌面操作系统有:Windows.Mac OS.ubuntu,其中Mac OS 和 ubuntu上都会自带python.这里我们只介绍下Windows(我用的Win10)环境下的pytho ...
- Topology and Geometry in OpenCascade-Adapters
Topology and Geometry in OpenCascade-Adapters eryar@163.com 摘要Abstract:本文简要介绍了适配器模式(adapter pattern) ...
- WPF根据Oracle数据库的表,生成CS文件小工具
开发小工具的原因: 1.我们公司的开发是客户端用C#,服务端用Java,前后台在通讯交互的时候,会用到Oracle数据库的字段,因为服务器端有公司总经理开发的一个根据Oracle数据库的表生成的cla ...
- 【原创】开源Math.NET基础数学类库使用(11)C#计算相关系数
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...