1

I've created a Client-Server application, and on the Server I want to have the oportunity to stop the server and then start it again. The problem is that I can't stop the Thread that listen for Tcp Connections.

How can I close a Thread in C#?

Thanks.

private void KeepServer(){
    while (this.connected)
    {
         tcpClient = tls.AcceptTcpClient();
         Connection newConnection = new Connection(tcpClient);
    }
}

4 Answers 4

8

In general, you should "stop" threads by indicating that you want them to stop, and letting them do it. It's recommended that you don't use Thread.Abort except for emergency situations where you're shutting down the whole application. (Calling Thread.Abort on the currently executing thread is safer, but still generally icky. That's what ASP.NET does when you redirect, by the way.)

I have a page about stopping threads gracefully. You don't have to use that exact code, of course - but the pattern of setting a flag and testing it periodically is the main point.

Now, how that gets applied in your particular situation will depend on how you're listening for TCP connections. If you could post the code used by that thread, we may be able to adapt it appropriately.

7
  • +1 This is good advice - what are the disadvantages of calling Thread.Abort? Does it cause hanging threads or memory leaks?
    – user195488
    Dec 16, 2009 at 19:07
  • 1
    Thread.Abort raises a ThreadAbortException on the thread, wherever execution is currently at. If the thread is running code that isn't expecting an exception - it could result in a lot of weird behavior.
    – LBushkin
    Dec 16, 2009 at 19:08
  • 1
    Worse, dealing with ThreadAbortException complicates resource cleanup and can result in either complicated/messy code or resource leaks. As Jon says, it's a good idea to avoid Thread.Abort().
    – LBushkin
    Dec 16, 2009 at 19:09
  • @Roboto: You don't know that the thread is at a safe place to abort, so it can leave your app in a "bad" state.
    – Jon Skeet
    Dec 16, 2009 at 19:13
  • @Jon: I looked at your example. Looks good, but wouldn't your example do the same?
    – user195488
    Dec 16, 2009 at 19:17
2

Yor Question is a little general, but, I think that this may help you:

Threads in C#

I paste a portion:

Stopping a Thread

Normally, when a thread is started, it runs until finished. However, it is possible to stop a thread by calling the Abort() method. In our example, if we want to stop firstThread, you would add the following code.

Read more at Suite101: How to Create, Stop and Suspend Threads in C# | Suite101.com http://www.suite101.com/article.cfm/c_sharp/96436#ixzz0ZsZRRjKx

Happy coding!

0

You should use a boolean or a condition to stop the Thread. You can than use a Property to change the "Flag" of this boolean and the loop of the Thread will end. This is a proper way to do it. Of course, you can use Abort() on the Thread but this is not recommended and will raise an Exception that you will need to handle.

0

possible from the outside Thread.Abort and there is a way to pause. but its an incorrect way to do it...

you should just end the code to kill, reach the last } . and for pausing you should use a Mutex. the manner of opporation should be, you order the object to pause, and after it finishes with the current request it gets halt by the mutex before the next one. or just steps out side of the while for "kill".

btw the MSDN has a nice article describing common threading scenarios

1
  • i haven't programmed C# for sevral months now... meant Monitor... but anything that can stop the thread and be triggered from the outside will work.... but yes monitor is nicer :-)
    – AK_
    Dec 16, 2009 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.