做项目的时候有时经常会需要一个带有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 功能的函数调用的更多相关文章

  1. [转载]Process工具类,提供设置timeout功能

    FROM:http://segmentfault.com/blog/lidonghao/1190000000372535 在前一篇博文中,简单介绍了如何使用Process类来调用命令行的功能,那样使用 ...

  2. jsContext全局函数调用与对象函数调用、evaluateScript

    evaluateScript:兼具js加载(生成具体的上下文)(函数与通用变量的加载),与函数执行的功能: 函数调用的方式有两种: 1)获取函数(对象),然后执行调用: [context[@" ...

  3. 让fetch也可以timeout

    原生的HTML5 API fetch并不支持timeout属性,习惯了jQuery的ajax配置的同学,如果一时在fetch找不到配置timeout的地方,也许会很纠结.fetch 的配置 API 如 ...

  4. 【ssm】spring功能讲解

    概览 Spring5框架包含许多特性,负责管理项目中的所有对象,并被很好地组织在下图所示的模块中 核心容器:由spring-beans.spring-core.spring-context.sprin ...

  5. 从linux源码看socket(tcp)的timeout

    从linux源码看socket(tcp)的timeout 前言 网络编程中超时时间是一个重要但又容易被忽略的问题,对其的设置需要仔细斟酌.在经历了数次物理机宕机之后,笔者详细的考察了在网络编程(tcp ...

  6. poll调用深入解析

    poll调用深入解析http://blog.csdn.net/zmxiangde_88/article/details/8099049 poll调用和select调用实现的功能一样,都是网络IO利用的 ...

  7. select、poll、epoll

    1.概念 select.poll.epoll都是事件触发机制,当等待的事件发生就触发进行处理,用于I/O复用 2.简单例子理解 3.select函数 3.1函数详解 int select(int ma ...

  8. Java并发编程系列-(9) JDK 8/9/10中的并发

    9.1 CompletableFuture CompletableFuture是JDK 8中引入的工具类,实现了Future接口,对以往的FutureTask的功能进行了增强. 手动设置完成状态 Co ...

  9. Linux Socket 网络编程

    Linux下的网络编程指的是socket套接字编程,入门比较简单.在学校里学过一些皮毛,平时就是自学玩,没有见识过真正的socket编程大程序,比较遗憾.总感觉每次看的时候都有收获,但是每次看完了之后 ...

随机推荐

  1. Ecshop 最小起订量如何设置

    第一步,商品表必须有个字段  代表某个商品 最小订购数量->min_number 打开goods表   在最后字段添加一个min_number  tinyint类型 默认值为0  代表没有最小起 ...

  2. Codeforces 346C Number Transformation II 构造

    题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> # ...

  3. Windows Tomcat7.0 安装 Solr

    准备工作 1.下载Tomcat7.0 ,apache-tomcat-7.0.67.exe,安装目录如下:C:\workspace\Tomcat7.0\ 2.下载Solr 5.2,solr-5.2.0. ...

  4. C++14 make code cleaner

    在C++11中我们如果要写一个通过tuple实现函数调用的函数要这样写: template<int...> struct IndexTuple{}; template<int N, ...

  5. [原]quick集成spine动画

    更新说明: 新增了骨骼绑定node用法 参考:http://blog.csdn.net/n5/article/details/21795265 在SkeletonRenderer.h 和cpp里面新加 ...

  6. saiku 展示优化

    saiku版本:3.7.4 下面是修改步骤,如果觉得麻烦,可以直接下载源代码:https://github.com/lihehuo/saiku 1.关闭自动执行 修改文件:saiku-ui/js/sa ...

  7. 许可EDM营销是个长期过程

    为什么这么说呢?基于博主自己这三四年的理解,许可EDM营销确实是个长期的过程,这跟一般的EDM营销有一定的区别. 大多数时候不会有立竿见影的效果,而且需要持续地不间断地进行到底,这也是很多企业实施许可 ...

  8. webkit内核分析之 Frame

    参考地址:http://blog.csdn.net/dlmu2001/article/details/6164873 1.    描述 Frame类是WebCore内核同应用之间联系的一个重要的类.它 ...

  9. react-native SyntaxError xxxxx/xx.js:Unexpected token (23:24)

    在运行react-native项目时提示 SyntaxError xxxxx/xx.js:Unexpected token (23:24) 我这边的问题原因:jsx语法错误,解决办法就是认真排查代码然 ...

  10. Application MyTest has not been registered. This is either due to a require() error during initialization or failure to call AppRegistry.registerComponent.

    运行react-native项目时报错. 说明一下:项目本来是好的,再次运行就报错了 解决解决办法倒是有,不过具体什么原因不知道.希望有知道具体原因的童鞋能够补充一下 第一种情况:真的是注册的时候写错 ...