I seem to remember that abort only works when the thread is in
wait/sleep/join state (i.e. doing some kind of wait operation) Not sure if
..Net thread wrapper changes that model or not. That said, aborting is
rather prute force. From your description, a better approach may be to have
your thread loop check a sync'd bool (or other) for stopped state. I
normally setup an State enum like Running, Stopped, Paused, etc. and check
that at the top of my loop. Then you can clean up and exit nicely. If your
thread still does not stop after some period of time (ie. 5 sec, etc) then
abort it and join with wait. I am not clear on what condition would cause
the thread not to respond to both your stop signal and the thread abort, but
things do go bump in the night, so may need to abort and wait, abort and
wait for some max times. At some point, you just have to give up and exit
with fatal error as something must be really wrong at that point.
--
William Stacey, MVP
Morris said:
I want to abort a running thread, so I call MyThread.abort() function. My
problem is this thread runs "almost" like a while(true) loop and I don't
want the Abort() function interrupts the thread at any point in the thread.
In fact, I have a section of code needs to be "protected" from being
interrupted. How can I make sure Abort() will not land anywhere winthin this
block? In other words, the Abort() must wait until this block of code is
done before it actually stop the thread.