这里真正棘手的部分是通过将执行程序线程从Action传递回可能中止的位置来杀死长期运行的任务。我通过使用包装的委托来完成此任务,该委托将线程传递出去以杀死生成lambda的方法中的局部变量。
我提交了这个示例,供您欣赏。您真正感兴趣的方法是CallWithTimeout。
这将通过中断长时间运行的线程并吞下ThreadAbortException来取消它 :
用法:
class Program{ static void Main(string[] args) { //try the five second method with a 6 second timeout CallWithTimeout(FiveSecondMethod, 6000); //try the five second method with a 4 second timeout //this will throw a timeout exception CallWithTimeout(FiveSecondMethod, 4000); } static void FiveSecondMethod() { Thread.Sleep(5000); }工作的静态方法:
static void CallWithTimeout(Action action, int timeoutMilliseconds) { Thread threadToKill = null; Action wrappedAction = () => { threadToKill = Thread.CurrentThread; try { action(); } catch(ThreadAbortException ex){ Thread.ResetAbort();// cancel hard aborting, lets to finish it nicely. } }; IAsyncResult result = wrappedAction.BeginInvoke(null, null); if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds)) { wrappedAction.EndInvoke(result); } else { threadToKill.Abort(); throw new TimeoutException(); } }}


