Is TcpClient thread safe ?

  • Thread starter Thread starter craigkenisston
  • Start date Start date
C

craigkenisston

I'm creating a TcpClient instance in an object which is created in a
thread.

I have a loop that goes about 6-7 cycles, creating the container object
in each cycle and each object contains a TcpClient instance.

If I run this loop without threading everything goes find. But if I do
it within the thread, invariable on of the threads will fail to
connect.

So, I was wondering whether TcpClient is safe to be used in this way ?
 
Hi,

It doesn't matter whether TcpClient is thread safe or not unless you are actually accessing an instance of TcpClient on multiple
threads. It sounds like you are creating one TcpClient per thread from your description.

Anyway, the docs say that instance members are not guaranteed to be thread-safe.

TcpClient on MSDN:
http://msdn2.microsoft.com/en-us/library/1612451t.aspx
 
Thanks a lot, Dave, that's exacty the one doubt I've ever had but never
seen it explained in a single paragraph like this :
 
Back
Top