如何中止长时间运行的方法?长时间、方法

2023-09-07 16:29:07 作者:正儿八经的小骚年°

我有一个长期运行的方法,我想补充超时进去。难道这样做可行吗?是这样的:

I have a long running method and I want to add timeout into it. Is it feasible to do that? Something like:

AbortWaitSeconds(20)
{
    this.LongRunningMethod();
}

在哪里,当它达到了20秒,该方法将被中止。该方法没有循环,我没有控制/ code在那个方法。

Where when it reached 20 seconds, the method will be aborted. The method doesn't have loop and I do not have a control/code over that method.

推荐答案

请参阅我回答这个问题一个通用解决方案。

See my answer to this question for a generic solution.