Re: how to kill a worker thread that doesn't do any work in a loop
On Tue, 15 Sep 2009 10:07:29 -0700 (PDT), Seetharam <smisro@gmail.com>
wrote:
I'm trying to fix a problem of slow data retrieval reading a file
across network.
I trying to move this "reading from file" code into a worker thread so
that my main app doesn't hang.
However I'm not sure how to kill this thread now. All examples and
tutorials on the web talk about setting an event from outside and
checking for this event state inside the threadproc and call
AfxEndThread(). But this "check" can happen only if the "work" is
being typically done in a loop. I have no while/for loop and I know
that the blocking function is the one "Serialize" call that tries to
read the file from the network.
Any suggestions?
If you can use non-blocking I/O, problem solved. Otherwise, I dunno. I
guess you might try TerminateThread, and if that doesn't work out, maybe
try doing the I/O in another process, using some type of IPC to communicate
it to your main process. I'm not sure which of these represents the method
of last resort. :)
P.S. AfxEndThread(), like all "exit" type functions, doesn't unwind the
stack, and so destructors for local variables won't be called. If you want
them to be called, you will need to return to your thread controlling
function and simply return from it. One way is to throw an exception and
catch it there, being careful not to eat it at any point along the way.
--
Doug Harrison
Visual C++ MVP