Requirements: A NET/C# method needs to use System.Diagnostics.Process to call other program operations, if a third-party programConstant lack of response can lead to methods that are always waitingBecause a third-party program may require you to enter a command, the method will not respond no matter how long it waits, resulting in the program not being able to continue executing normally.
Regarding setting a timeout time for a method, you should consider a few things:
- The method has timed out, but it may still be enforced
- If the timeout continues, will it fill up the thread? Full memory? Full CPU?
- The method timed out, how to gracefully end the execution of the method
I saw some code on the Internet, such as about Task timeout, although the effect was achieved, butTask will continue to be executed, which can consume resources all the time, causing the program to crash.
The code is as follows:
Reviewing the topic, this article sets the timeout time for the methodTargeted and not suitable for all methods, just to provide an idea, please modify it according to your actual situation.
Our method calls System.Diagnostics.Process to start a third-party program, and once the method times out, we want to kill the process that terminates the third-party program! The code is as follows:
(End)
|