Question

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?

Was it helpful?

Solution

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; 

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top