.NET Stream Killing

I created a thread that executes a specific method. But sometimes I would like to kill a thread, even if it still works. How can i do this? I tried Thread.Abort (), but it found a message with the message "Thread aborted". What should I do?

+19
multithreading c # abort
Jun 27 '09 at 0:38
source share
7 answers

Do not call Thread.Abort() !

Thread.Abort is dangerous. Instead, you should collaborate with the thread so that you can safely disconnect it. The stream must be designed so that it can be killed, for example, by having the boolean keepGoing flag, which you set to false when you want to stop the stream. Then the stream will have something like

 while (keepGoing) { /* Do work. */ } 

If the thread can block in Sleep or Wait , then you can break it into these functions by calling Thread.Interrupt() , then the thread should be ready to handle ThreadInterruptedException :

 try { while (keepGoing) { /* Do work. */ } } catch (ThreadInterruptedException exception) { /* Clean up. */ } 
+47
Jun 27 '09 at 0:51
source share

You really should only call Abort () as a last resort. You can use a variable to synchronize this stream:

 volatile bool shutdown = false; void RunThread() { while (!shutdown) { ... } } void StopThread() { shutdown = true; } 

This allows your thread to cleanly complete what it is doing, leaving your application in a known good condition.

+25
Jun 27 '09 at 0:47
source share

The most correct and thread-safe way is to use WaitHandle to feed the signal when it should stop. I mainly use ManualResetEvent.

In your topic, you can:

 private void RunThread() { while(!this.flag.WaitOne(TimeSpan.FromMilliseconds(100))) { // ... } } 

where this.flag is an instance of ManualResetEvent. This means that you can call this.flag.Set() from outside the thread to stop the loop.

The WaitOne method will return true only when the flag is set. Otherwise, it will expire after the specified timeout (100 ms in the example), and the thread will again go through the loop.

+8
Jun 27 '09 at 6:20
source share

It is not recommended to kill the stream. Better to say that he should stop and let him finish gracefully. There are various ways to do this.

  • Use Thread.Interrupt to pop it if it is locked.
  • Interrogate a flag variable.
  • Use the WaitHandle class to send a signal.

I do not need to rephrase how each method can be used, as I already did this in this answer .

+7
Sep 20 '10 at 17:39
source share

Canceling a thread is a very bad idea, since you cannot determine what the thread was doing during the interrupt.

Instead, have a property that the thread can check, and that your external code can be set. Let the thread verify this logical property when it is in a safe exit location.

+2
Jun 27 '09 at 0:49
source share

I agree with John B

 volatile bool shutdown = false; void RunThread() { try { while (!shutdown) { /* Do work. */ } } catch (ThreadAbortException exception) { /* Clean up. */ } } void StopThread() { shutdown = true; } 
+2
May 19 '12 at 11:55
source share

There are also examples of thread destruction in my WebServer class ...

https://net7ntcip.codeplex.com/SourceControl/changeset/view/89621#1752948

I would say that Abort is fine, just to understand what forks are ... while you indicate the state before long work, Abort will work, but you need flags like (ShouldStop or ActionBranch, etc.)

Check out the examples!

+1
May 19 '12 at 11:59
source share



All Articles