From: Jordan Moss on
Hey,

My question is regarding multithreading in CF.net and safe cleanup

I have a class that exposes events when first caller hooks the event I
create a thread that blocks on a Win32 handle when the handle is set I invoke.

My question is regarding safe exit of the threads, I have code in the
destructor that will unblock the thread and wait for it to exit as the
finally of the thread handles cleanup of Win32 msgqueue etc.

The problem I have is that if I have Thread.IsBackground=false the
destructor is never called, if I set it to true then application exits but it
will not call the destructor first to unblock the threads instead it raises a
ThreadAbortException.

I have had a quick look at IDisposable pattern but I don't believe this is
the answer as it requires users to make calls to dispose? I would have
thought the framework would have made this call when class is out of scope
and has no references?

Can someone please enlighten on how I should implement so that I can get a
call on destruction to safely unblock threads so they don't get aborted.

Cheers,
Jordan M.

From: "Paul G. Tobey [eMVP]" paul tobey _AT_ earthlink _DOT_ on
You can use one of the closing events on your main form, or you can add code
to the main routine. I've actually never tried to do that in the destructor
of anything; I put the thread shutdown in the close event of the form
responsible for the thread, trying to keep the shutdown logically related to
where the thread was started.

Paul T.

"Jordan Moss" <JordanMoss(a)discussions.microsoft.com> wrote in message
news:ACDC990C-A692-4CDA-ADBC-26BBF7A21865(a)microsoft.com...
> Hey,
>
> My question is regarding multithreading in CF.net and safe cleanup
>
> I have a class that exposes events when first caller hooks the event I
> create a thread that blocks on a Win32 handle when the handle is set I
> invoke.
>
> My question is regarding safe exit of the threads, I have code in the
> destructor that will unblock the thread and wait for it to exit as the
> finally of the thread handles cleanup of Win32 msgqueue etc.
>
> The problem I have is that if I have Thread.IsBackground=false the
> destructor is never called, if I set it to true then application exits but
> it
> will not call the destructor first to unblock the threads instead it
> raises a
> ThreadAbortException.
>
> I have had a quick look at IDisposable pattern but I don't believe this is
> the answer as it requires users to make calls to dispose? I would have
> thought the framework would have made this call when class is out of scope
> and has no references?
>
> Can someone please enlighten on how I should implement so that I can get a
> call on destruction to safely unblock threads so they don't get aborted.
>
> Cheers,
> Jordan M.
>


From: Han on

How about catching the ThreadAbortException and unblock the threads there ?

"Jordan Moss" wrote:

> Hey,
>
> My question is regarding multithreading in CF.net and safe cleanup
>
> I have a class that exposes events when first caller hooks the event I
> create a thread that blocks on a Win32 handle when the handle is set I invoke.
>
> My question is regarding safe exit of the threads, I have code in the
> destructor that will unblock the thread and wait for it to exit as the
> finally of the thread handles cleanup of Win32 msgqueue etc.
>
> The problem I have is that if I have Thread.IsBackground=false the
> destructor is never called, if I set it to true then application exits but it
> will not call the destructor first to unblock the threads instead it raises a
> ThreadAbortException.
>
> I have had a quick look at IDisposable pattern but I don't believe this is
> the answer as it requires users to make calls to dispose? I would have
> thought the framework would have made this call when class is out of scope
> and has no references?
>
> Can someone please enlighten on how I should implement so that I can get a
> call on destruction to safely unblock threads so they don't get aborted.
>
> Cheers,
> Jordan M.
>