Multiple concurrent asynchronous writes on a Stream (NetworkStream

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

Guest

Here's a (I think) simple question:

If I issue multiple async writes on a NetworkStream, is the data guaranteed to be sent in the correct order?

i.e.

stream.BeginWrite(.. data A ...);
stream.BeginWrite(.. data B ...);

Will data A always be completely sent before B is sent?

I figure/guess no, but it would be useful if BeginWrite could be used like this...

Thanks for listening
 
There is no way that it could be assured that it would ALWAYS be in the
correct order. Remember that BeginWrite spawns a new thread, and which
thread starts in what order (particularly in this case as it is part of
the ThreadPool - so you might end up with it actually being queued...)
is determined be the Operating System.

HTH

David
 
cheers for that.

As I suspected - it is up to the programmer to take responsibility for ensuring correct sequence of data..

So I should package data A & B into one buffer and write that, and ensure it's completed before issuing another Write or BeginWrite (which is what my code does at present anyway...)

cheers,
 
I would most likely put the data in a Queue then have a secondary thread
that reads the queue when the stream is available. This why I would
insure that the writes occur in the order specified and still maintain
all of the requirements for processing speed and "perceived"
performance.

HTH

David
 
Back
Top