How to prevent endless loop in ASP.net

On ASP.NET 4.0 web pages, I would like to detect and prevent code that is too long. Therefore, I am looking for such a construction:

try for 1000 ms { RunPotentiallyTooLongCode(); } catch { RecordError( "code ran out of control" ); // let user know ... } 

Although we are currently using 4.0, I will also be interested in solutions for 4.5, perhaps the added async features will help.

+6
source share
1 answer

You will create a new thread for a long-term task, then block and wait until this thread completes or until a timeout is reached. If the timeout has been reached, then the task is apparently blocked (either an endless loop, a dead end, or blocking I / O waiting), and you can end the stream.

 Thread thread = new Thread( RunPotentiallyTooLongCode ); thread.Start(); Boolean success = thread.Join( 1000 ); if( !success ) thread.Abort(); 

There is no need to use asynchronous material 4.0. In fact, this code will work fine on .NET 1.0 as well.

+6
source

Source: https://habr.com/ru/post/924634/


All Articles