473,837 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

6 New Member
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;
  7.     // Create and start a new worker thread
  8.     Thread workerThread = new Thread( new ThreadStart( workerObject.RunAS400Program ) );
  9.     workerThread.IsBackground = true;
  10.     workerThread.Start();
  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;
  7.     // Create and start a new worker thread
  8.     Thread workerThread = new Thread( new ThreadStart( workerObject.RunAS400Program ) );
  9.     workerThread.IsBackground = true;
  10.     workerThread.Start();
  12.     while ( !workerThread.IsAlive ) { }; // Let the thread start up before we check on it
  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.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.

Feb 15 '08 #1
1 3310
6 New Member
Does System.Threadin g.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

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

Similar topics

by: Max Samookha | last post by:
Hi! Does anybody know how to abort a long-running asynchronous call from the calling thread? To do this, one needs a reference to the thread executing the call and there seems to be no easy way to get this reference from the calling thread. You can do the following but I don't like that much: .... // calling thread Thread asyncCallThread;
by: ll | last post by:
Hi, How to specify the timeout value(ms) for a thread? Thanks.
by: JeffSinNHUSA via DotNetMonster.com | last post by:
Hi, I have a long running task that I want to run on a regular basis to do some db updates. I can't write a windows service because it is being hosted elsewhere. I want to run this task within my web application - so here is what I did: In the global.asax I declared a delegate function and a timer. In the application start event, I started the timer. I created a method to respond to the timer event, which creates a new thread and...
by: oshinonline | last post by:
Hi, In my application I have RWTPtrSlist<POSIXThread>, where POSIXThread is my wrapper class. Whenever I create a new thread I call append on the list. But it hangs after I created 11/12 threads. Here is stack Trace: Thread 5 (system thread 3513041): #0 0x1342f0 in shi_allocSmall2+0xa4 () #1 0x133c58 in MemAllocPtr+0xb8 () #2 0x1391fc in shi_New+0x80 ()
by: Jon | last post by:
My question is: Can the Garbage Collector (GC) suspended a managed thread while it is executing native code. The reason I am interested in this is that I have: 1) a native thread (N) that only executes native code, 2) a managed thread (M) that sometimes executes native code, 3) a critical section, that is shared by the native code portions of N & M. And I what to know: Can N be suspend by the GC? This would be true if the GC could...
by: Andrue Cope | last post by:
Hi, We have a strange problem. We are invoking the FolderBrowseDialog and have found that if we cancel that dialog within a few seconds of it appearing it causes one of our (completely unrelated) background threads to hang on program shut down. Details: Background thread is sitting idle waiting for work. This thread has nothing to with the folder selection activity - it's a completely
by: abid gee | last post by:
Please give a kind look on my question. and please comments. I am Using C# as development tool of Dot Net 2.0. I wrote a function read_data() that read data from Serial Port continuously.Till application gets close. But When Form loaded read_data() function take tooo much CPU. Even I can not see Application controls. Some body told me, Use Threading.
by: care02 | last post by:
Hi! I have the following problem: I have written a short Python server that creates an indefinite simulation thread that I want to kill when quitting (Ctrl-C) from Python. Googling around has not given me any hints on how to cleanly kill running threads before exiting. Any help is appreciated! Carl
by: Kevin | last post by:
Hi! Is there anyway to set a timeout for a Thread? Regards!
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.