首页 > Windows开发 > 详细

C# 指定方法的超时时间,超出时限后,直接抛出异常

时间:2015-11-13 13:07:00      阅读:385      评论:0      收藏:0      [点我收藏+]
        public static void Invoke(Action method, int milliseconds)
        {
            Thread thdToKill = null;

            Action invokemethod = new Action(() =>
            {
                thdToKill = Thread.CurrentThread;
                method();
            });

            IAsyncResult ar = invokemethod.BeginInvoke(null, null);
            if (!ar.AsyncWaitHandle.WaitOne(milliseconds))
            {
                thdToKill.Abort();
                throw new Exception(string.Format("操作失败,原因:超时 {0}毫秒", milliseconds));
            }

            invokemethod.EndInvoke(ar);
        }

 

C# 指定方法的超时时间,超出时限后,直接抛出异常

原文:http://www.cnblogs.com/xyz0835/p/4961740.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!