I want to kill a thread when something very bad happen. What’s your suggestion to do thread suicide?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You should never actually “kill” a thread. From your main program, you can abort it using the Thread.Abort method, but this is not recommended, and it is completely unnecessary.
Your thread should be nothing more than a method executing a long loop. If something “really bad” happens, simply return from this method and thread will end.
This would be enough for such a loop:
On the other hand, if you need your main program to kill the thread (but this is not a “suicide” as you named it), then you should signal the thread that you want it to end, and not abort it without knowing what is going on in the background.