4

I created a thread and that thread can be suspended. So, how do I kill or terminate a suspended thread?

I tried to ABORT the thread and I got a runtime error message saying that the thread is suspended and it can't be aborted. I've looked for terminate method or something similar and it doesn't seem to exist.

myThread := new Thread(@BigLoop);
myThread.Start;

myThread.Suspend;
myThread.Abort; <<<===exception is raised.

So, how do you kill or terminate a suspended thread?

Ken White
  • 123,280
  • 14
  • 225
  • 444
ThN
  • 3,235
  • 3
  • 57
  • 115

2 Answers2

1

After I resumed the suspended thread, I was able to abort the thread;

myThread := new Thread(@BigLoop);
myThread.Start;

myThread.Suspend;


if MyThread.ThreadState = ThreadState.Suspended then
   myThread.Resume;

myThread.Abort; 
ThN
  • 3,235
  • 3
  • 57
  • 115
0

Note that it's unadvisable to use abort. A better solution would be to use a waithandle (autoresetevent/manualresetevent) to notify the thread that it should stop running. Remember that "Abort" doesn't work on anything that calls into native code, like com or pinvoke.

Carlo Kok
  • 1,128
  • 5
  • 14
  • 1
    Ck. That's true but what if you are trying to abort a thread on your program shutdown. Does it matter then? – ThN Dec 23 '12 at 15:05