Hi Tom
Yes, I should have been more specific. This isn't a good replacement for
terminating a thread in an orderly manner. That said, I have found recently
that using a flag to indicate that the thread does not always work. My
example is where I set my flag to false (to terminate the thread) and then
use thread Join to wait for it to terminate. It never does. If I allow the
application to run on, or use a DoEvents (ugh!), then the thread terminates.
I now use the thread Abort method and trap the exception raised in the
thread. It's not pretty, but it works, and permits some sort of graceful
exit.
Charles
[Incidently, I currently have a problem based on the non-terminating issue
above, that involves SyncLock. There are three threads, including the main
UI thread, all trying to send and receive commands down a serial port. All
commands go out through a single function, making it easier to protect.
However, while the two background threads are hammering away at sending and
receiving commands, if I try to send a command from the UI the UI thread
freezes trying to get a lock on my command queue. Once it does that I can't
find out what the other threads are doing in the debugger, as any
breakpoints I set in the body of the threads are never hit. I'm not sure I
have given you enough to go on, but any suggestions about debugging
multi-threaded apps greatly received.]
Tom Shelton wrote:
On Wed, 26 May 2004 11:12:19 +0100, Charles Law wrote:
Hi Li Pang
When you create the thread, set IsBackground = True before you start
it running. That way it will be terminated when the owning thread
terminates.
HTH
Charles
Charles,
I'm not so sure this is a good idea... I have to check, but I
believe if you do this then the threads are forcibly terminated -
which means that they are not given an opertunity to run any clean up
code. Personally, I would set a flag to signal the threads to
terminate, and then wait for the children to die before exiting.