Worker thread preventing application shutdown

B

Bill Davidson

All:

I have a problem in which a worker thread in my (.dll) assembly isn't
allowing the main (.exe) assembly from terminating.

Here's the scenario:

1) App.Exe is launched.
2) App.Exe calls into my .dll (Sub.Dll)

This call causes the method in Sub.Dll to spawn a worker thread that I want
to hang around (monitoring the status of an environment parameter) for as
long as the parent process lives; i.e. until the main thread in App.Exe
returns/exits, the close button clicked on the Window frame, etc.

The problem is that I can't seem to find any way to get notification when
this main application wants to terminate. As such, my worker thread is
preventing App.Exe from closing.

I've tried subscribing to the App Domain's ProcessExit event, but that event
didn't fire in my subordinate assembly.

Any help would be appreciated.

Thanks,
Bill
 
P

Peter Duniho

[...]
This call causes the method in Sub.Dll to spawn a worker thread that I
want
to hang around (monitoring the status of an environment parameter) for as
long as the parent process lives; i.e. until the main thread in App.Exe
returns/exits, the close button clicked on the Window frame, etc.

The problem is that I can't seem to find any way to get notification when
this main application wants to terminate. As such, my worker thread is
preventing App.Exe from closing.

Can you be more explicit about what behavior it is exactly that you need?

If you mark the thread as a "background" thread (it's a property on the
Thread class), then the thread will be automatically terminated when the
application wants to exit. Of course, the thread won't get any sort of
notification of termination with this method.

If you need for the thread to cleanup before the application exits, then
you'll need to include some kind of synchronization and signaling between
the main application and the thread. One of the simplest mechanisms for
doing this would be to create an event that the thread checks periodically
(with a 0 length timeout so that the thread doesn't actually wait on it).
If the event gets signaled, then the thread stops what it's doing, cleans
up, and exits (which will then allow the whole application to exit).

If neither of the above solutions are useful to you, then I think you're
going to need to post more details about what you're doing and how you
want things to work.

Pete
 
G

Guest

If the alternate thread is marked IsBackGround =true, then you should be
able to call the Join method, as long as there aren't other bad coding habits
you've inadvertently injected.
Peter
 
B

Bill Davidson

Thanks to you both. Setting the 'IsBackground' property did the trick.

Cheers,
Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top