前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 代码中实现函数超时流程

C# 代码中实现函数超时流程

作者头像
Shunnet
发布2021-06-11 15:29:55
1.1K0
发布2021-06-11 15:29:55
举报

记录一下怕要用时忘记

直接上代码

代码语言:javascript
复制
        /// <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);
            /// <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();
                    }
                };
            }
            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;
            }
        }

实现流程

代码语言:javascript
复制
        TimeoutChecker timeout = new TimeoutChecker(
            delegate
            {
	        //要运行的函数

            },
            delegate
            {
                 //超时处理位置
                 
            });
            if (timeout.Wait(5000))
            {
                 //正常
            }

本文作者:[博主]大顺

本文链接:https://shunnet.top/qumQBj

版权声明:转载注明出处,谢谢

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-04-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档