无回路终止线程立即中止没有暂停或 [英] Terminate loopless thread instantly without Abort or Suspend

查看:162
本文介绍了无回路终止线程立即中止没有暂停或的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现一个协议库。在这里,一个简单的描述。

I am implementing a protocol library. Here a simplified description.

在主函数中的主线程会经常检查,是否可在该部分的NetworkStream数据(TcpClient的范围内)。让我们说反应是收到消息,线程是一个正在运行的线程。

The main thread within the main function will always check, whether some data is available on the the networkstream (within a tcpclient). Let us say response is the received message and thread is a running thread.

thread = new Thread(new ThreadStart(function));
thread.IsBackground = true;
thread.Start();

while(true){

response = receiveMessage();

if (response != null)
     {                
      thread.Suspend();
      //I am searching for an alternative for the line above and not thread.Abort().

      thread2 = new Thread(new ThreadStart(function2));
      thread2.IsBackground = true;
      thread2.Start();         
     }
}



到目前为止好,其实有更多的消息来来while循环内,并且也有用于处理不同的排序传入消息的的statemachine,但这应该是足够的。
(也有不只是功能的功能和函数2等等)。

So far so good, there are actually more messages to come within the while loop and there is also a statemachine for handling different sort of incoming messages, but this should be enough. (There are also more than just the functions "function" and "function2").

那么,反正怎样的功能看里面是不是在这个应用程序明确中,由于协议从程序员隐藏,意味着是一个库。这意味着,在协议的程序是什么状态取决于该协议将启动一些程序员定义的函数作为一个线程。

So anyways how the functions look inside is not clear in this application, since the protocol is hidden from the programmer and meant to be a library. This means the protocol will start some programmer-defined functions as a thread depending on at what state in the protocol the program is.

所以,如果再一个特殊的应答(例如,一个callAnotherFunction消息),我想终止
A螺纹(这里命名为线程)突然,让说在100毫秒。但我不知道是否在一个循环中有无和多少处理直到需要它终止执行。

So if then a special response is received (e.g. a callAnotherFunction message), I want to terminate a thread (here named "thread") abruptly, lets say within 100 ms. But I do not know whether it executes within a loop or without and how much processing is needed until it terminates.

如何停止这些线程没有过时暂停或Exceptionthrowing中止功能?
(请注意,我不能强迫的功能程序员赶上ThreadAbortException。)

How to stop these threads without deprecated Suspend or Exceptionthrowing Abort function? (Note that I cannot force the programmer of the functions to catch the ThreadAbortException.)

或者我需要不同的程序架构?
(顺便说一句,我决定把内环路receiveMessage轮询网络流进主函数,因为随时随地的消息会出现)。

Or do I need a different programme architecture? (Btw I have decided to put the loop within receiveMessage for polling the network stream into the main function, since anytime a message can appear).

推荐答案

启动一个线程,而无需一个可靠的方式来终止它是一种不好的做法。暂停/中止是那些不可靠的方法来终止线程,因为你可能会终止在破坏你的整个程序的状态的线程,你有没有办法来避免这样的事情发生的。

Starting a thread without having a reliable way to terminate it is a bad practice. Suspend/Abort are one of those unreliable ways to terminate a thread because you may terminate a thread in a state that corrupts your entire program and you have no way to avoid it from happening.

您可以看到如何杀死一个线程安全的位置:杀死一个线程(C#)

You can see how to kill a thread safely here: Killing a thread (C#)

如果用户是给你在一个线程中运行的方法,那么用户还应该给你一个方法停止运行的代码。把它看成是一个契约:你答应,你将调用stop方法的用户,他们承诺停止方法实际上将停止线程。如果您的用户违反了合同,然后,他们将负责出现的,这是很好的问题,因为你不想负责你的用户的错误。)

If the "user" is giving you a method to run in a thread, then the user should also give you a method to stop the code from running. Think of it as a contract: you promise the user that you will call the stop method and they promise that the stop method will actually stop the thread. If your user violates that contract then they will be responsible for the issues that arise, which is good because you don't want to be responsible for your user's errors :).

请注意,我不能强迫的功能程序员赶上ThreadAbortException。

Note that I cannot force the programmer of the functions to catch the ThreadAbortException.

由于暂停/中止是不好的做法,程序员不需要赶 ThreadAbortException ,但他们应该抓住 ThreadInterruptedException 作为其一部分他们的合同

Since Suspend/Abort are bad practice, the programmer doesn't need to catch the ThreadAbortException, however they should catch the ThreadInterruptedException as part of their "contract."

请记住,有两种情况,你需要担心的:

Remember that there are two situations you need to worry about:


  1. 的线程执行一些代码。

  2. 线程处于阻塞状态。

在的情况下,该线程执行一些代码,所有你能做的就是通知它可以退出并等待它处理通知的线程。您也可以跳过等待和假设你已经泄露的资源,在这种情况下,再次是用户的错,因为他们没有设计他们的止损方法来终止其及时线程。

In the case that the thread is executing some code, all you can do is notify the thread that it can exit and wait until it processes the notification. You may also skip the waiting and assume that you've leaked a resource, in which case it's the user's fault again because they didn't design their stop method to terminate their thread in a timely fashion.

在当线程处于阻塞状态的情况下,它不是阻塞通知结构(即信号,手动复位事件,等等),那么你应该叫了Thread.interrupt( )来把它弄出来的阻塞述明,用户必须办理 ThreadInterruptedException

In the case where the thread is in a blocking state and it's not blocking on a notification construct (i.e. semaphore, manual reset event, etc) then you should call Thread.Interrupt() to get it out of the blocking state- the user must handle the ThreadInterruptedException.

这篇关于无回路终止线程立即中止没有暂停或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆