C#超时处理(转载)
/// <summary>
/// 超时处理
///
///
/// </summary>
public class TimeoutChecker
{
long _timeout; //超时时间
System.Action<Delegate> _proc; //会超时的代码
System.Action<Delegate> _procHandle; //处理超时
System.Action<Delegate> _timeoutHandle; //超时后处理事件
System.Threading.ManualResetEvent _event = new System.Threading.ManualResetEvent(false);
public TimeoutChecker(System.Action<Delegate> proc, System.Action<Delegate> timeoutHandle)
{
this._proc = proc;
this._timeoutHandle = timeoutHandle;
this._procHandle = delegate
{
//计算代码执行的时间
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
if (this._proc != null)
this._proc(null);
sw.Stop();
//如果执行时间小于超时时间则通知用户线程
if (sw.ElapsedMilliseconds < this._timeout && this._event != null)
{
this._event.Set();
}
};
}
public bool Wait(long timeout)
{
this._timeout = timeout;
//异步执行
this._procHandle.BeginInvoke(null, null,null);
//如果在规定时间内没等到通知则为 false
bool flag = this._event.WaitOne((int)timeout, false);
if (!flag)
{
//触发超时时间
if (this._timeoutHandle != null)
this._timeoutHandle(null);
}
this.Dispose();
return flag;
}
private void Dispose()
{
if(this._event != null)
this._event.Close();
this._event = null;
this._proc = null;
this._procHandle = null;
this._timeoutHandle = null;
}
}
调用超时处理方法:
/// <summary>
/// 检查摄像头是否可用
/// </summary>
/// <param name="device">所选设备</param>
/// <param name="image">摄像头输出,用于判断</param>
/// <returns>image不为空摄像头可用,否则不可用</returns>
public bool isCameraWork(Camera device, NImage image)
{
try
{
device.StartCapturing();
TimeoutChecker timeout = new TimeoutChecker(
delegate
{
try
{
image = device.GetCurrentFrame();
}
catch
{
image = null;
nlView.Image = null;
}
},
delegate
{
Console.WriteLine(device.ToString() + "获取设备超时");
});
if (timeout.Wait(1000))
Console.WriteLine(device.ToString() + " 设备获取成功");
}
catch (Exception e)
{
image = null;
Console.WriteLine(device.ToString() + e.Message);
}
device.StopCapturing();
if (image != null)
return true;
else
return false;
}
C#超时处理(转载)的更多相关文章
- ajax请求超时判断(转载)
ajax请求时有个参数可以借鉴一下 var ajaxTimeOut = $.ajax({ url:'', //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : ' ...
- 如何设置ASP.NET页面的运行超时时间 (转载)
全局超时时间 服务器上如果有多个网站,希望统一设置一下超时时间,则需要设置 Machine.config 文件中的 ExecutionTimeout 属性值.Machine.config 文件位于 % ...
- [ 转载]JAVA Socket超时浅析
JAVA Socket超时浅析 转载自 http://blog.csdn.net/sureyonder/article/details/5633647 套接字或插座(socket)是一种软件形 式的抽 ...
- libcurl网络连接使用tcp/ip
CURL *curl; CURLcode res; const char *request = "GETas.xxxxE测试发送"; curl_socket_t sockfd; / ...
- python urllib2介绍
urllib2是Python的一个获取URLs(Uniform Resource Locators)的组件.他以urlopen函数的形式提供了一个非常简单的接口, 这是具有利用不同协议获取URLs的能 ...
- Linux系统出现hung_task_timeout_secs和blocked for more than 120 seconds的解决方法
Linux系统出现系统没有响应. 在/var/log/message日志中出现大量的 “echo 0 > /proc/sys/kernel/hung_task_timeout_secs" ...
- 【转载】Extjs设置Ajax请求的超时时间timeout
在Extjs中的Ajax请求中,Ext.Ajax.request 默认超时时间是30秒,有时候我们有比较耗时的操作需要设置更长时间,此时我们就需要修改Ext.Ajax.Requset的超时时间为更长, ...
- druid socket timeout超时15分钟(转载)
背景 在应用端通过mybatis的interceptor自定义Plugin拦截Executor, 统计输出sql的执行耗时. 今天生产发生一个很奇怪的问题: 莫名其妙卡顿15分钟+,其后正常返回sql ...
- 如何设置ASP.NET页面的运行超时时间 (转载)
全局超时时间 服务器上如果有多个网站,希望统一设置一下超时时间,则需要设置 Machine.config 文件中的 ExecutionTimeout 属性值. Machine.config 文件位于 ...
随机推荐
- 转:char*, char[] ,CString, string的转换
转:char*, char[] ,CString, string的转换 (一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准 ...
- 修改weblogic11g的JDK版本
1:进入Weblogic域文件夹下面 [wzh@localhost bin]$ pwd/app/wzh/oracle/middleware/user_projects/domains/base_dom ...
- rm加转义很危险
rm -r 想转义一个空格字符,转得不对 -r, -R, --recursive 递归删除目录及其内容 连续rm了n个不想rm的文件夹.%>_<% 想起来以前有人也因为rm的失误把整个wo ...
- 【HDU3081】Marriage Match II (二分+最大流)
Description Presumably, you all have known the question of stable marriage match. A girl will choose ...
- sublime sftp 插件安装及破解
必备插件sftp: 1,Package Control可以用来install其他package 2,sftp远程编辑文件:安装:install->sftp具体配置 sftp工具破解 1, ...
- windows提权操作以及系统开机关机重启代码(用到了LookupPrivilegeValue和AdjustTokenPrivileges调整进程的Token权限)
对于UAC提权操作,一般在编译期间,如果程序有需求要提权,会在编译器里设置,vs2010比较简单,在工程属性里可以直接设置,vs2005稍微有点儿麻烦,参考这篇文章: http://www.seany ...
- OpenWrt挂载USB储存设备实现Samba共享
没有USB接口的路由器不是好路由器,有了USB接口OpenWrt才有更多的玩法,比如挂载U盘.移动硬盘等USB储存设备实现Samba共享,打造小型家庭服务器. 1.安装与USB相关的软件包: opkg ...
- [LeetCode#246] Missing Ranges Strobogrammatic Number
Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...
- delphi调用webservice 转
如今 Web Service 已越来越火了,在DotNet已开发的Web Service中,Delphi 7如何方便的调用DotNet写的Web Service呢?方法有两种,一种是在Delphi ...
- 自己记录java环境搭建之-maven
简述: 现需要在Eclipse中配置Maven插件,同时安装maven应用,配置Maven环境变量,建立Maven管理的工程,并用Maven导入Gson包, 编写简易Json输出程序 步骤: 1. 首 ...