Threading problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have to send several messages at the same time and wait for the replies,
so I use a thread for each message.

//Get all the data at the same time.
foreach(Thread thread in threads) thread.Start();
//Wait till all the data is retrieved.
foreach(Thread thread in threads) thread.Join();

And this works... sometimes :/
All the threads always start ok, so all the messages are sent out ok.
But the main thread doesn't always wait for all the threads to finish... The
main thread sometimes continues, even though I haven't received all my
replies.
Why is this?

The only thing I can come up with, is that it might be because of when i
spin, waiting for an answer.
while (!stream.DataAvailable ){}
Maybe if all the threads but one last one are finished, and the last one is
still waiting for an answer, then this last thread might go to sleep or
something, causing the main thread to continue?
Or is this not possible?

I have no clue at all on how to solve this.
 
that's not really possible. Once you call join, main waits.
Your while loop do? wait for threads to flush their contents to the stream?
explain that part, i believe your problem lies in this routine.

--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
 
My loop doesn't do anything untill it gets an answer.

But I have found the problem. I assumed that if one of the threads sending
the messages caused an unhandled exception, that it would reach the main
thread. That is not the case, that thread is just stopped, and when all the
threads have stopped (normally or because of an exception) the main thread
continues as if nothing went wrong.
Therefor I didn't know a timeout exception had happened, and in that case it
is normal that there isn't a reply :)

Thanks for your willingness to help!
 
Back
Top