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.
source share