具有timeout 功能的函数调用
做项目的时候有时经常会需要一个带有timeout功能的函数调用。
比如从后台读数据并期望在给定时间内返回。借此机会包装了一个简单的C# class, 直接上代码吧.
public class TimeoutInvokerWrapper
{
private ManualResetEvent mTimeoutObject;
private bool mBoTimeout;
private Func<object, object> doHandler; public string Error { get; private set; }
public object Result { get; private set; } public TimeoutInvokerWrapper(Func<object, object> handler)
{
this.doHandler = handler;
this.mTimeoutObject = new ManualResetEvent(true);
} public bool DoWithTimeout(object input, TimeSpan timeSpan)
{
if (this.doHandler == null)
{
return false;
} this.mTimeoutObject.Reset();
this.mBoTimeout = true; this.doHandler.BeginInvoke(input, DoAsyncCallBack, null);
if (!this.mTimeoutObject.WaitOne(timeSpan, false))
{
this.mBoTimeout = true;
} return this.mBoTimeout;
} private void DoAsyncCallBack(IAsyncResult result)
{
try
{
this.Result = this.doHandler.EndInvoke(result);
this.mBoTimeout = false;
}
catch (Exception ex)
{
this.mBoTimeout = true;
this.Error = ex.ToString();
}
finally
{
this.mTimeoutObject.Set();
}
}
}
该类可以这样使用
public static void Main(string[] args)
{
Func<object, object> func = (obj) => { return obj.ToString(); }; TimeoutInvokerWrapper wrapper = new TimeoutInvokerWrapper(func);
bool isTimeout = wrapper.DoWithTimeout(123, TimeSpan.FromSeconds(2));
if (isTimeout)
{
System.Console.WriteLine("function call timeout.");
}
else if (!string.IsNullOrEmpty(wrapper.Error))
{
System.Console.WriteLine("function call does not timeout but throw exception.");
}
else
{
System.Console.WriteLine("call succeed.")
System.Console.WriteLine(wrapper.Result);
}
}
具有timeout 功能的函数调用的更多相关文章
- [转载]Process工具类,提供设置timeout功能
FROM:http://segmentfault.com/blog/lidonghao/1190000000372535 在前一篇博文中,简单介绍了如何使用Process类来调用命令行的功能,那样使用 ...
- jsContext全局函数调用与对象函数调用、evaluateScript
evaluateScript:兼具js加载(生成具体的上下文)(函数与通用变量的加载),与函数执行的功能: 函数调用的方式有两种: 1)获取函数(对象),然后执行调用: [context[@" ...
- 让fetch也可以timeout
原生的HTML5 API fetch并不支持timeout属性,习惯了jQuery的ajax配置的同学,如果一时在fetch找不到配置timeout的地方,也许会很纠结.fetch 的配置 API 如 ...
- 【ssm】spring功能讲解
概览 Spring5框架包含许多特性,负责管理项目中的所有对象,并被很好地组织在下图所示的模块中 核心容器:由spring-beans.spring-core.spring-context.sprin ...
- 从linux源码看socket(tcp)的timeout
从linux源码看socket(tcp)的timeout 前言 网络编程中超时时间是一个重要但又容易被忽略的问题,对其的设置需要仔细斟酌.在经历了数次物理机宕机之后,笔者详细的考察了在网络编程(tcp ...
- poll调用深入解析
poll调用深入解析http://blog.csdn.net/zmxiangde_88/article/details/8099049 poll调用和select调用实现的功能一样,都是网络IO利用的 ...
- select、poll、epoll
1.概念 select.poll.epoll都是事件触发机制,当等待的事件发生就触发进行处理,用于I/O复用 2.简单例子理解 3.select函数 3.1函数详解 int select(int ma ...
- Java并发编程系列-(9) JDK 8/9/10中的并发
9.1 CompletableFuture CompletableFuture是JDK 8中引入的工具类,实现了Future接口,对以往的FutureTask的功能进行了增强. 手动设置完成状态 Co ...
- Linux Socket 网络编程
Linux下的网络编程指的是socket套接字编程,入门比较简单.在学校里学过一些皮毛,平时就是自学玩,没有见识过真正的socket编程大程序,比较遗憾.总感觉每次看的时候都有收获,但是每次看完了之后 ...
随机推荐
- Mac OS X Terminal 101:终端使用初级教程
Mac OS X Terminal 101:终端使用初级教程 发表于 2012 年 7 月 29 日 由 Renfei Song | 文章目录 1 为什么要使用命令行/如何开启命令行? 2 初识Com ...
- PD16 Generate Datebase For Sql2008R2时报脚本错误“对象名sysproperties无效”
PowerDesinger16创建数据库表到SQL2008R2时,执行报“对象名sysproperties无效”错误. 主要是在建模时我们对表.列增加了些说明注释,而Sql2005之后系统表syspr ...
- (转)数字格式化函数:Highcharts.numberFormat()
一.函数说明 该函数用于图表中数值的格式化,常见用途有数值精度控制.小数点符.千位符显示控制等. 二.函数使用 1.函数构造及参数 Highcharts.numberFormat (Numbe ...
- Emit动态生成代码
Emit动态生成代码 引用:秒懂C#通过Emit动态生成代码 首先需要声明一个程序集名称, // specify a new assembly name var assemblyName = new ...
- C_中使用SendMessage
SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...
- java 流
http://www.iteye.com/magazines/132-Java-NIO http://liyuanning.blog.163.com/blog/static/4573228620101 ...
- Axapta 3 COM Connector
Axapta 3 COM Connector Copy from blog Having fun setting up the COM+ connector for Enterprise Port ...
- 用对 gitignore
使用 git 做代码管理工具时,设置 gitignore 是必不可少的流程,一些系统或者 IDE 会在目录下生成与项目不相关的文件,而这些文件我们不期望被提交到仓库之中.理解 gitignore 的 ...
- 关于windows的service编程
最近需要学习下windows的service编程框架,查了下msdn发现不知所云.于是谷歌之,发现了一个非常不错的文章,重点推荐讲的非常详细,深入,看完之后基本上就能很清楚windows的servic ...
- Android关联源码support-v4的问题解决
如果在有用到过viewpager或者fragmentActivity等一些v4包下的类,当我们按F3时无法查看到源码,这个时候就需要我们关联该源码,该源码的关联与android源码的关联不一样. 大家 ...