Is TcpClient thread safe ?

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 ?
 
D

Dave Sexton

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
 
C

craigkenisston

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 :
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top