• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#超时处理(转载)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 /// <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;
        }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C语言程序设计第四次作业——选择结构(2)发布时间:2022-07-13
下一篇:
objective-cNSArray列出指定文件目录列表发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap