Implementing a timeout on a function,实现对一个方法执行时间进行限制
2012-03-21 11:45
633 查看
4.0版
2.0版
referer:
http://stackoverflow.com/questions/1370811/implementing-a-timeout-on-a-function-returning-a-value http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/
public static T Execute<T>(Func<T> func, int timeout) { T result; TryExecute(func, timeout, out result); return result; } public static bool TryExecute<T>(Func<T> func, int timeout, out T result) { var t = default(T); var thread = new Thread(() => t = func()); thread.Start(); var completed = thread.Join(timeout); if (!completed) thread.Abort(); result = t; return completed; }
2.0版
public delegate object ExecuteMethod(object parameter); public static object Execute(ExecuteMethod executeMethod, object parameter, int timeout) { object result; TryExecute(executeMethod, parameter, timeout, out result); return result; } public static bool TryExecute(ExecuteMethod executeMethod,object parameter, int timeout, out object result) { object r=null; var thread = new Thread(() => r = executeMethod(parameter)); thread.Start(); var completed = thread.Join(timeout); if (!completed) thread.Abort(); result = r; return completed; }
referer:
http://stackoverflow.com/questions/1370811/implementing-a-timeout-on-a-function-returning-a-value http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/
相关文章推荐
- 限制某方法执行时间的一个解决方案和实现
- C#在VS2005开发环境中利用异步模式来对一个方法的执行时间进行超时控制
- 一个可以限制执行时间的命令timeout
- python 限制函数执行时间,自己实现 timeout
- C#: 一个方法执行超时 timeout 检查的实现
- 你知道的,javascript语言的执行环境是"单线程模式",这种模式的好处是实现起来比较简单,执行环境相对单纯;坏处是只要有一个任务耗时很长,后面的任务都必须排队等着,会拖延整个程序的执行,因此很多时候需要进行“异步模式”,请列举js异步编程的方法。
- 编写程序,实现读文件,计算方法执行平均时间
- 当JAVA WEB服务器端有一个执行时间长的方法,应该如何处理?
- spring aop 实现方法执行时间监控
- 借用.net framework的string.Fromat(...),实现一个执行参数化SQL的方法
- 点某个链接进网站,会发这个链接的会员加积分,实现方法(有时间限制)
- 在一个组织单元中对不同对象进行策略限制的方法对比
- C#测试代码、函数、方法执行时间,方便进行系统性能评估
- 用js实现在加载完成一个页面后自动执行一个方法
- 使用ASM4.0实现AOP的功能,监控每个方法的执行时间
- Python实现监控程序执行时间并将其写入日志的方法
- python实现对一个完整url进行分割的方法
- 我们公司接到了一个比较大的IT软件项目,由于项目时间紧急,我们想采用有非常手段、非常方法,不自己进行全部的开发工作。
- 实现一个栈(元素遵守先入后出顺序),能够通过 min 方法在 O(1)时间内获取栈中的最小元素。同时,栈的基本操作:入栈(Push)、出栈(Pop),也是在O(1)时间内完成的
- 实现分析sql语句执行过程和编译时间的方法