Hi, i have a problem. I have 2 threads in my app, the main process
app and a background worker. In the background thread i have a top level
try catch block. I need to know best way to clean up handles to the
background
thread if it causes an exception.

try{
while (g_fContinue){
WaitForSingleObject(hEvent, TIMEOUT);
//some code
}
return 0;
}
catch(Exception& e){
e.ErrorMsg();
return 0;
}

To exit the thread from the main thread i just set the g_fContinue flag and
event
and then clean up but if it faults on its own where do i clean up. Can
i call CloseHandle(hThread) in the catch block?

Re: Closing down thread after exception by Ulrich

Ulrich
Tue Nov 28 01:43:57 CST 2006

{Note: setting f'up to mpwev, this has nothing to do with PB.}

dev15 wrote:
> In the background thread i have a top level try catch block. I need
> to know best way to clean up handles to the background thread if it
> causes an exception.
>
> try{
> while (g_fContinue){
> WaitForSingleObject(hEvent, TIMEOUT);
> //some code
> }
> return 0;
> }
> catch(Exception& e){
> e.ErrorMsg();
> return 0;
> }

FYI: I catch exceptions as reference-to-const, although that doesn't matter
much.

> To exit the thread from the main thread i just set the g_fContinue flag
> and event and then clean up but if it faults on its own where do i
> clean up.

In C++, typically the so-called RAII idiom is used.

> Can i call CloseHandle(hThread) in the catch block?

Yep, manual cleanup is possible, although it won't work if an unknown
exception is thrown and it is tedious and error-prone. I see no reason to
do it.

Uli