Click here to Skip to main content
11,647,659 members (73,569 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# socket
Dear All: Code:
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. Thanks.
Posted 20-Aug-12 5:57am
Edited 20-Aug-12 6:17am
Wes Aday103.2K
Wes Aday at 20-Aug-12 12:01pm
That makes no sense at all. Calling Sleep will not dispose your connection for you.
Sergey Alexandrovich Kryukov at 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. --SA
ridoy at 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 at 21-Aug-12 1:03am
5+ thanks for Thread.Sleep(0) explanation.
Sergey Alexandrovich Kryukov at 21-Aug-12 14:19pm
Thank you, Amit. --SAq
Kuthuparakkal at 21-Aug-12 1:32am
great solution... so much to learn from you SA
Sergey Alexandrovich Kryukov at 21-Aug-12 14:19pm
Thank you very much for your nice words. --SA
Sam_lin at 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? Thanks.
Sergey Alexandrovich Kryukov at 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... --SA
Sergey Alexandrovich Kryukov at 21-Aug-12 14:31pm
Anyway, if you agree it makes sense, please accept the answer formally (green button) -- thanks. --SA
Sam_lin at 21-Aug-12 22:50pm
Sergey Alexandrovich Kryukov at 22-Aug-12 0:49am
You are very welcome. Good luck, call again. --SA

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

  Print Answers RSS
0 500
1 F-ES Sitecore 370
2 OriginalGriff 322
3 DamithSL 315
4 CPallini 240
0 OriginalGriff 1,342
1 994
2 DamithSL 971
3 Sergey Alexandrovich Kryukov 873
4 CPallini 795

Advertise | Privacy | Mobile
Web01 | 2.8.150804.3 | Last Updated 21 Aug 2012
Copyright © CodeProject, 1999-2015
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