473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Worker thread preventing application shutdown

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
Feb 13 '07 #1
3 3262
On Mon, 12 Feb 2007 16:57:30 -0800, Bill Davidson
<Ra************@newsgroup.nospamwrote:
[...]
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
Feb 13 '07 #2
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

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Bill Davidson" wrote:
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
Feb 13 '07 #3
Thanks to you both. Setting the 'IsBackground' property did the trick.

Cheers,
Bill
Feb 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Joey Powell | last post by:
Re: Original post = Windows forms - how do I get them to render/update properly? from August 22. Okay I am making some progress with being able to use delegates to run my shelled processes on...
5
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker...
2
by: Dave | last post by:
I have an application that queries SQL Server and Index Server using ADO. I'd like to have a feature where a user can cancel a long-running query. What I plan on doing is to move my query code to a...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
0
by: dan kisting | last post by:
Hello, I have written this C# application that is causing Windows to not shut down. That is not the intention of course. Cicking Shutdown on Windows causes the app to quit, but you have to hit...
2
by: Artem | last post by:
When I use the method Thread.Abort, it only sends a request of aborting to OS to stop a thread. The thread itself isn't killed and allocated resources aren't released. I tried to run that thread...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
1
by: nicerun | last post by:
I'm using the Application_Start event at Global.asax.cs to invoke thread that do some job. I know that Application_Start event occurs when the very first request to Web Application received. -...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.