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

时间:2015-11-13 13:07:00   收藏:0   阅读:385
        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);
        }

 

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!