C# 线程管理:超时定时任务创建

Posted 数据轨迹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 线程管理:超时定时任务创建相关的知识,希望对你有一定的参考价值。

public static void InvokeTimeOut(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# 线程管理:超时定时任务创建的主要内容,如果未能解决你的问题,请参考以下文章