简体   繁体   English

如何在C#中停止线程?

[英]How can I stop a thread in C#?

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. 问题是我无法停止侦听Tcp Connections的线程。

How can I close a Thread in C#? 如何在C#中关闭线程?

Thanks. 谢谢。

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

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. 建议不要使用Thread.Abort除非在紧急情况下要关闭整个应用程序。 (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.) (在当前执行的线程上调用Thread.Abort较为安全,但通常还是比较麻烦的。顺便说一下,这就是ASP.NET的工作。)

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. 现在,如何将其应用于您的特定情况将取决于您侦听TCP连接的方式。 If you could post the code used by that thread, we may be able to adapt it appropriately. 如果您可以发布该线程使用的代码,我们也许可以对其进行适当调整。

Yor Question is a little general, but, I think that this may help you: 您的问题有点笼统,但是,我认为这可以帮助您:

Threads in C# 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. 但是,可以通过调用Abort()方法来停止线程。 In our example, if we want to stop firstThread, you would add the following code. 在我们的示例中,如果要停止firstThread,则应添加以下代码。

Read more at Suite101: How to Create, Stop and Suspend Threads in C# | 在Suite101上了解更多信息:如何在C#中创建,停止和挂起线程。 Suite101.com http://www.suite101.com/article.cfm/c_sharp/96436#ixzz0ZsZRRjKx Suite101.com http://www.suite101.com/article.cfm/c_sharp/96436#ixzz0ZsZRRjKx

Happy coding! 编码愉快!

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. 当然,您可以在线程上使用Abort(),但是不建议这样做,它会引发您需要处理的异常。

possible from the outside Thread.Abort and there is a way to pause. 可能是从Thread.Abort外部获取的,有一种方法可以暂停。 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. 为了暂停,您应该使用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 顺便说一句,MSDN上有一篇不错的文章介绍了常见的线程方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM