This article is a mirror article of machine translation, please click here to jump to the original article.

View: 9355|Reply: 5

[Source] [Actual combat]. NET/C# sets a timeout time for a method

[Copy link]
Posted on 5/4/2022 6:59:51 PM | | | |
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)





Previous:[Actual combat]. NET/C# calls 7z decompression via the command line
Next:Nginx uses HTTPS to configure SSL certificates
Posted on 5/4/2022 11:10:31 PM |
Learn to learn...
Posted on 10/10/2022 1:12:08 PM |
Learn to learn...
Posted on 10/10/2022 1:12:50 PM |
Learn it
Posted on 10/10/2022 1:13:27 PM |
Let's take a look at the solution of the big guy hee hee
 Landlord| Posted on 10/10/2022 5:22:12 PM |
Task.WaitAsync() API in .NET 6

In .NET 6, a new WaitAsync method has been added, which is used to wait asynchronously for a task to complete, and when waiting asynchronously, you can specify a Timeout time or a cancellation token CancellationToken, in the previous version there was only a synchronous Wait that would wait for the task to complete, and did not support better task timeouts or cancellations Extensions such as TimeoutAfter can replace these extensions with WaitAsync

https://andrewlock.net/a-deep-di ... nc-api-in-dotnet-6/
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com