How to abort a long running method?
问题 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(); } 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. 回答1: See my answer to this question for a generic solution. 回答2: try this class Program { static void Main(string[] args) { if (RunWithTimeout(LongRunningOperation, TimeSpan.FromMilliseconds(3000))