线程运行超时处理类

线程运行超时处理类

    public class TimeoutChecker
    {
        #region /*private member*/

        long _timeout; //超时时间
        System.Action<Delegate> _proc;//会超时的代码
        System.Action<Delegate> _procHandle;//处理超时
        System.Action<Delegate> _timeoutHandle;//超时后处理事件
        System.Threading.ManualResetEvent _event = new System.Threading.ManualResetEvent(false);

        #endregion

        #region /*constructor method*/

        /// <summary>
        /// 构选方法
        /// </summary>
        /// <param name="proc">会超时的代码</param>
        /// <param name="timeoutHandle">超时后处理事件</param>
        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();
                }
            };
        }

        #endregion

        #region /*public method*/

        /// <summary>
        /// 等待执行
        /// </summary>
        /// <param name="timeout">等待时间毫秒</param>
        /// <returns></returns>
        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;
        }

        #endregion

        #region private method

        /// <summary>
        /// 释放资源
        /// </summary>
        private void Dispose()
        {
            if (this._event != null)
                this._event.Close();
            this._event = null;
            this._proc = null;
            this._procHandle = null;
            this._timeoutHandle = null;
        }

        #endregion
    }

调用

 

 TimeoutChecker timeout = new TimeoutChecker(
                    delegate
                    {
                        
                        try
                        {
                            //把耗时的处理加到这里
                           
                        }
                        catch (Exception he)//异常处理
                        {
                           
                        }
                    },
                    delegate//超时处理
                    {
                     
                    });

                // 按毫秒等待
                // 执行成功的处理,且未超时
                if (timeout.Wait(10000))
                {
                    Console.WriteLine("WaitTimes: {0}", DateTime.Now.ToString());
                }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110454.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • datagrip激活码20213月最新在线激活

    datagrip激活码20213月最新在线激活,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月14日
    36
  • Java和Python哪个更好?

    Java和Python哪个更好?一些开发人员声称Python比Java更有效率。但这应该先弄清Python和Java之间的区别是什么?Java和Python的区别Java是一种严格的类型语言,这意味着必须显式声明变量名。相比之下,动态类型的Python则不需要声明变量。在编程语言上有许多关于动态和静态类型的争论,但有一点应该注意:Python是一种语法简单的功能强大的语言,能够通过编写脚本就提供优秀的解决方案,并能够快捷…

    2022年7月8日
    15
  • C#数组–(一维数组,二维数组的声明,使用及遍历)

    C#数组–(一维数组,二维数组的声明,使用及遍历)数组:是具有相同数据类型的一组数据的集合。数组的每一个的变量称为数组的元素,数组能够容纳元素的数称为数组的长度。一维数组:以线性方式存储固定数目的数组元素,它只需要1个索引值即可标识任意1个数组元素

    2022年7月2日
    21
  • Nginx + Tomcat 搭建负载均衡

    Nginx + Tomcat 搭建负载均衡Nginx + Tomcat 搭建负载均衡

    2022年4月22日
    39
  • BeanUtils中copyProperties的使用[通俗易懂]

    BeanUtils中copyProperties的使用[通俗易懂]BeanUtils中copyProperties的作用是将一个对象中的属性值赋值(拷贝)给另一个对象中对应的属性。其中赋值成功的属性对应的属性名和属性类型必须相同,否则对应的属性值不会从一个对象赋值给另一个对象,但是此时不影响其他属性值的拷贝。1、实体类publicclassModel01{privateStringname;privateintage;

    2022年10月3日
    0
  • 2.session.setAttribute()和session.getAttribute()区别和联系

    2.session.setAttribute()和session.getAttribute()区别和联系2.session.setAttribute和session.getAttribute()区别和联系在web开发的时候,使用的都是B/S架构,浏览器与服务器直接连接,在服务端就会自动创建一个session对象.。session.setAttribute(“username”,username);》》是将username保存在session中!session的key值为“usern…

    2022年10月17日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号