Does StreamWriter.Flush() block?

  • Thread starter Thread starter Simon Johnson
  • Start date Start date
S

Simon Johnson

I have a TCP Client class for which I use StreamWriter to send data
across the network. The question I have is does the StreamWriter.Flush()
method block code execution until all the data has been sent to the client?

Simon.
 
Simon Johnson said:
I have a TCP Client class for which I use StreamWriter to send data
across the network. The question I have is does the StreamWriter.Flush()
method block code execution until all the data has been sent to the
client?

It will block until the underlying stream's Flush method has finished,
I'd expect. What exactly that means depends on the stream, but I'd
expect it to have been sent to (but not necessarily read by) the
client.
 
Jon said:
It will block until the underlying stream's Flush method has finished,
I'd expect. What exactly that means depends on the stream, but I'd
expect it to have been sent to (but not necessarily read by) the
client.

Is the way around this to use async sockets.. is there like a
BeginSend() method or something?

Simon.
 
Simon Johnson said:
Is the way around this to use async sockets.. is there like a
BeginSend() method or something?

I'm not sure what you're trying to achieve. If you're trying to avoid
blocking whatever happens, then indeed asynchronous IO is the way
forward... and a quick look at the MSDN shows that yes, there is a
Socket.BeginSend method.
 
You'll lose the ability to use a StreamWriter if you want asynchronous IO.
AFAIK there isn't a way to use a StreamWriter and still do asynchronous IO
out the other side.
 
Justin Rogers said:
You'll lose the ability to use a StreamWriter if you want asynchronous IO.
AFAIK there isn't a way to use a StreamWriter and still do asynchronous IO
out the other side.

Hmm... I hadn't noticed that before. Slightly odd. It wouldn't be
particularly difficult to rewrite the routines appropriately, I
suspect. All you really need to do is encode the data and then write it
asynchronously...
 
Back
Top