Hi all,
I have a problem with with Thread.Join( Timeout ) where the timeout never occurs.
I basically need to make a connection to an AS400 box which works fine. Once in a blue moon the AS400 gets a problem and the way this is handled on AS400 is by hanging. If I was to connect directly this would mean my main process would hang as well, so I've spawned a worker thread and used a Thread.Join( Timeout ) to make sure it always returns with someting after a while.
The problem is that if the worker thread with the AS400 call hangs, the Timeout never occurs.
So I went the other way and made my own monitor. Spawn a thread and do a while loop on the main thread and check Thread.IsAlive (ie whether the worker thread has finished) every 100ms until Timeout.
This however always hangs the AS400 call, even with the AS400 calls that returns correctly with Thread.Join.
My own monitor always returns though, but always returns a false as the AS400 calls fails. If I substitute the AS400 calls with a simple System.Threadin g.Thread.Sleep( 1000) in the WorkerClass it all works fine and dandy.
Some will now say the AS400 call class isn't working, but it is if I use Thread.Join. Hmmmm...?
Any help very welcome before I lose all my hair, which by now is all grey anyway.
Thanks,
Chrace
I have a problem with with Thread.Join( Timeout ) where the timeout never occurs.
I basically need to make a connection to an AS400 box which works fine. Once in a blue moon the AS400 gets a problem and the way this is handled on AS400 is by hanging. If I was to connect directly this would mean my main process would hang as well, so I've spawned a worker thread and used a Thread.Join( Timeout ) to make sure it always returns with someting after a while.
The problem is that if the worker thread with the AS400 call hangs, the Timeout never occurs.
Code:
private bool RunProgramThread() { // Create a new worker object to carry out the AS400 call, required for timeouts AS400WorkerClass workerObject = new AS400WorkerClass(); workerObject.program = program; workerObject.parameters = parameters; // Create and start a new worker thread Thread workerThread = new Thread( new ThreadStart( workerObject.RunAS400Program ) ); workerThread.IsBackground = true; workerThread.Start(); return workerThread.Join( 10000 ); }
This however always hangs the AS400 call, even with the AS400 calls that returns correctly with Thread.Join.
Code:
private bool RunProgramThread() { // Create a new worker object to carry out the AS400 call, required for timeouts AS400WorkerClass workerObject = new AS400WorkerClass(); workerObject.program = program; workerObject.parameters = parameters; // Create and start a new worker thread Thread workerThread = new Thread( new ThreadStart( workerObject.RunAS400Program ) ); workerThread.IsBackground = true; workerThread.Start(); while ( !workerThread.IsAlive ) { }; // Let the thread start up before we check on it // Check every 100ms to see if thread has finished. If timeout, drop it and move on int timeOutDelay = 10000; DateTime runUntil = DateTime.Now.Add( new TimeSpan( 0, 0, 0, 0, timeOutDelay ) ); while ( ( DateTime.Now < runUntil ) && workerThread.IsAlive ) { System.Threading.Thread.Sleep( 100 ); } if ( workerThread.IsAlive ) { // If worker thread is still alive it failed to finish in time // Send an abort if the thread execution has timed out workerThread.Abort(); return false; } else { // Worker thread has finished correctly return true; } }
Some will now say the AS400 call class isn't working, but it is if I use Thread.Join. Hmmmm...?
Any help very welcome before I lose all my hair, which by now is all grey anyway.
Thanks,
Chrace
Comment