468,135 Members | 1,436 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,135 developers. It's quick & easy.

Thread.Join( Timeout ) hanging when executing 3rd party software

6
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.

Expand|Select|Wrap|Line Numbers
  1. private bool RunProgramThread() {
  2.     // Create a new worker object to carry out the AS400 call, required for timeouts
  3.     AS400WorkerClass workerObject = new AS400WorkerClass();
  4.     workerObject.program = program;
  5.     workerObject.parameters = parameters;
  6.  
  7.     // Create and start a new worker thread
  8.     Thread workerThread = new Thread( new ThreadStart( workerObject.RunAS400Program ) );
  9.     workerThread.IsBackground = true;
  10.     workerThread.Start();
  11.  
  12.     return workerThread.Join( 10000 );
  13. }
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.

Expand|Select|Wrap|Line Numbers
  1. private bool RunProgramThread() {
  2.     // Create a new worker object to carry out the AS400 call, required for timeouts
  3.     AS400WorkerClass workerObject = new AS400WorkerClass();
  4.     workerObject.program = program;
  5.     workerObject.parameters = parameters;
  6.  
  7.     // Create and start a new worker thread
  8.     Thread workerThread = new Thread( new ThreadStart( workerObject.RunAS400Program ) );
  9.     workerThread.IsBackground = true;
  10.     workerThread.Start();
  11.  
  12.     while ( !workerThread.IsAlive ) { }; // Let the thread start up before we check on it
  13.  
  14.     // Check every 100ms to see if thread has finished. If timeout, drop it and move on
  15.     int timeOutDelay = 10000;
  16.     DateTime runUntil = DateTime.Now.Add( new TimeSpan( 0, 0, 0, 0, timeOutDelay ) );
  17.     while ( ( DateTime.Now < runUntil  ) && workerThread.IsAlive ) {
  18.         System.Threading.Thread.Sleep( 100 );
  19.     }
  20.     if ( workerThread.IsAlive ) { // If worker thread is still alive it failed to finish in time
  21.         // Send an abort if the thread execution has timed out
  22.         workerThread.Abort();
  23.         return false;
  24.     }
  25.     else {
  26.         // Worker thread has finished correctly
  27.         return true;
  28.     }
  29. }
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.Threading.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
Feb 15 '08 #1
1 2959
Chrace
6
Does System.Threading.Thread.Sleep( x ) sleep any subthreads created by the main thread?

I have hooked this up to a Windows form system test harness and it seems that as soon as my self-developed timeout system exits the worker thread exits as well, so something is getting released. Or rather, something is getting blocked while I am doing my checking.

If the worker thread is blocked because of the sleep on the main thread it would make sense as the exit of the worker thread would then happen slightly after the main finishes execution.

Still doesn't explain that it can exit if I just use Sleep on the worker thread instead of the AS400 call.

Hmm, guess I answered my own question there.
Feb 15 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by ll | last post: by
4 posts views Thread by JeffSinNHUSA via DotNetMonster.com | last post: by
1 post views Thread by Andrue Cope | last post: by
2 posts views Thread by abid gee | last post: by
3 posts views Thread by Kevin | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.