What is the best way to wait for a thread to end?
I have a class and it's main purpose in life is to communicate with a device through TCP/IP. I have a method called Receive() which I start in a new thread immediately before I send data to the device (the device will answer back). In the past I have been using Join(iTimeout), where iTimeout is the number of milliseconds I'll wait before I am sick of waiting for a response (aka timeout). Because of harware and sortware on the device I might have to have a timeout of 6 seconds (6000ms) which I think is very long time (a normal communication takes 300ms).
The reason I don't like Join() is that it makes the app unresponsive until the thread ends or until the timeout has occured.
So what I did was to add logic into the receive method to terminate (end) itself if it has been running for a given number for milliseconds, but I want the caller to wait until the thread (Receive() method) is done before continuing.
One way is to do this:
while (oThread.IsAlive)
{
Sleep(0);
}
Is there another way? Or should I use Join()?
Any thoughts about what I am trying to do is welcome.
Thanks,
Einar
Semper ubi sub ubi.