C#超时处理

10 篇文章 0 订阅
在网上搜索了很多C#超时处理的方法,下面一种是我调试过的:
[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2. /// 超时处理  
  3. ///  
  4. ///  
  5. /// </summary>  
  6. public class TimeoutChecker  
  7. {  
  8.     long _timeout;              //超时时间  
  9.     System.Action<Delegate> _proc;               //会超时的代码  
  10.     System.Action<Delegate> _procHandle;         //处理超时  
  11.     System.Action<Delegate> _timeoutHandle;      //超时后处理事件  
  12.     System.Threading.ManualResetEvent _event = new System.Threading.ManualResetEvent(false);  
  13.   
  14.     public TimeoutChecker(System.Action<Delegate> proc, System.Action<Delegate> timeoutHandle)  
  15.     {              
  16.         this._proc = proc;  
  17.         this._timeoutHandle = timeoutHandle;  
  18.         this._procHandle = delegate  
  19.         {  
  20.             //计算代码执行的时间  
  21.             System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();  
  22.             sw.Start();  
  23.             if (this._proc != null)  
  24.                 this._proc(null);  
  25.             sw.Stop();  
  26.             //如果执行时间小于超时时间则通知用户线程  
  27.             if (sw.ElapsedMilliseconds < this._timeout && this._event != null)  
  28.             {  
  29.                 this._event.Set();  
  30.             }  
  31.         };              
  32.     }  
  33.     public bool Wait(long timeout)  
  34.     {  
  35.         this._timeout = timeout;  
  36.         //异步执行  
  37.         this._procHandle.BeginInvoke(nullnull,null);  
  38.         //如果在规定时间内没等到通知则为 false  
  39.         bool flag = this._event.WaitOne((int)timeout, false);  
  40.         if (!flag)  
  41.         {  
  42.             //触发超时时间  
  43.             if (this._timeoutHandle != null)  
  44.                 this._timeoutHandle(null);  
  45.         }  
  46.         this.Dispose();             
  47.   
  48.         return flag;  
  49.     }          
  50.     private void Dispose()  
  51.     {         
  52.         if(this._event != null)  
  53.             this._event.Close();  
  54.         this._event = null;  
  55.         this._proc = null;  
  56.         this._procHandle = null;  
  57.         this._timeoutHandle = null;                
  58.     }          
  59. }  


调用超时处理方法:
[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2.         /// 检查摄像头是否可用  
  3.         /// </summary>  
  4.         /// <param name="device">所选设备</param>  
  5.         /// <param name="image">摄像头输出,用于判断</param>  
  6.         /// <returns>image不为空摄像头可用,否则不可用</returns>  
  7.         public bool isCameraWork(Camera device, NImage image)  
  8.         {  
  9.             try  
  10.             {  
  11.                 device.StartCapturing();  
  12.                 TimeoutChecker timeout = new TimeoutChecker(  
  13.                     delegate  
  14.                     {  
  15.                         try  
  16.                         {  
  17.                             image = device.GetCurrentFrame();  
  18.                         }  
  19.                         catch  
  20.                         {  
  21.                             image = null;  
  22.                             nlView.Image = null;  
  23.                         }  
  24.                     },  
  25.                     delegate  
  26.                     {  
  27.                         Console.WriteLine(device.ToString() + "获取设备超时");  
  28.                     });  
  29.                 if (timeout.Wait(1000))  
  30.                     Console.WriteLine(device.ToString() + " 设备获取成功");  
  31.             }  
  32.             catch (Exception e)  
  33.             {  
  34.                 image = null;  
  35.                 Console.WriteLine(device.ToString() + e.Message);  
  36.             }  
  37.             device.StopCapturing();  
  38.             if (image != null)  
  39.                 return true;  
  40.             else  
  41.                 return false;  
  42.         }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值