I have run into situations where the blocking call I'm making isn't guaranteed to terminate itself ever. I want control over how long I wait before giving up.
private static void RunSync(int timeout, Action toDo)
{
Exception threadException=null;
Action wait = ( ) =>
{
try
{
toDo( );
}
catch (Exception ex)
{
threadException=ex;
}
};
var thread = new Thread(( ) => wait( ));
thread.Start( );
if (!thread.Join(timeout))
{
thread.Abort( );
throw new TimeoutException("No connection found within timeout:"+timeout/100+" seconds");
}
if (threadException!=null)
throw threadException;
}
This function is set up to wait for another multi-threaded class to try to find a connection and use it. If the timeouts on that class are set improperly or the threading code is bad and just never does a timeout or return, this gives me the option to stop waiting.
No comments:
Post a Comment