Click here to Skip to main content
12,953,743 members (47,568 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Dear All:


While(tcpClient.Available >0)
  // todo: read contents

The problem is

when call Thread.Sleep(0).

the tcpClient will be disposed.

the tcpClient.Available will throw exception.

Posted 20-Aug-12 5:57am
Updated 20-Aug-12 6:17am
Wes Aday104.2K
Wes Aday 20-Aug-12 12:01pm
That makes no sense at all. Calling Sleep will not dispose your connection for you.
Sergey Alexandrovich Kryukov 20-Aug-12 12:31pm
Basically, you are right, but it might indirectly manifest already existing problem in a wrong code, if some race condition is present. Please see the explanation in my answer.
ridoy 20-Aug-12 12:24pm
Question isn't clear..what you need to do here?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The problem, if any, is somewhere else. Yes, I'm 100% sure.

I little clarification: Thread.Sleep(0) is not equivalent to "no operation" or the code where this line is missing. To best of my knowledge, it takes some time for the method call, it cause switching the calling thread off and preemption by some other thread (perhaps one-time), and the actual delay is some random delay more then zero. It could not possibly disrupt operation of your thread.

However, it can indirectly manifest some problem with some probability, if you whole code design is wrong due to race condition you might embed in your design. I prefer more descriptive term "incorrect dependency on the time of execution". Please see:[^].

To start the investigation, you should learn about threading and parallel design from this point of view. You did not provide us enough information to help you.

@amitgajjar 21-Aug-12 1:03am
5+ thanks for Thread.Sleep(0) explanation.
Sergey Alexandrovich Kryukov 21-Aug-12 14:19pm
Thank you, Amit.
Kuthuparakkal 21-Aug-12 1:32am
great solution... so much to learn from you SA
Sergey Alexandrovich Kryukov 21-Aug-12 14:19pm
Thank you very much for your nice words.
Sam_lin 21-Aug-12 11:55am
i know what you mean, when call Thread.Sleep(0),There is a chance for opration system to switch another thread. That's said, another thread will close the socket.

i will check the whole code. Thanks.

But i want to make sure that "Thread.Sleep(0)" dosen't dispose any resource,right?

Sergey Alexandrovich Kryukov 21-Aug-12 14:30pm
Right, it cannot change anything in resource or whatever else, can only affect timing. If this call effects any operation logically, it means there is a race condition. Imagine that one thread adds 3 to some integer object, and another one multiplies the same object by 4. Even though if everything is correctly synchronized, the final result depends on which thread comes first, that is, the result is unpredictable. This is the race condition. It's a good idea to work with every socket or a network stream in one thread, but one thread can and usually should handle just one socket.

It does not mean you cannot share sockets between threads at all. On server end, the typical schema is: one thread is listening for new connections, and one more (or few threads) is working with network streams through the sockets, so the socket collection should be interlocked (using .NET lock, or something).

About closing sockets: I developed the practice to close sockets only on exceptions (+ on exit). Say, when a client terminates, the server side gets exception while trying to send data to the remote socket. Here is why: unpredicted closing of connections are unavoidable in principle (if a client computer looses power accidentally, you will get the same exception), so you can use unified closing mechanism for all connections, without the need for "graceful" disconnection. However, if it is essential to distinguish between graceful and accidental losses of connection, add some graceful mechanism in application-level protocol...

Sergey Alexandrovich Kryukov 21-Aug-12 14:31pm
Anyway, if you agree it makes sense, please accept the answer formally (green button) -- thanks.
Sam_lin 21-Aug-12 22:50pm
You are very welcome.
Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,429
CHill60 3,490
Maciej Los 3,103
ppolymorphe 2,020
Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 21 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100