Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / October 2007

Tip: Looking for answers? Try searching our database.

Foreground thread count detection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Doug Semler - 02 Oct 2007 16:45 GMT
Is there any way to detect/count the number of application defined
foreground threads that are currently active?

Background:  We have a library that processes application requests in
the background via a queuing mechanism (basically the library has one
thread that does the work, and the application's calls into the
library just puts the item to be processed on a queue).  Before you
say it, no MSMQ is not an option.  The problem is that there may be
items remaining in the queue when the application shuts down...that
need to be flushed.  If I make the worker thread a foreground thread,
an application will never shut down completely because the CLR doesn't
shutdown an app until all foreground threads have exited.  If I make
it a background thread, and the application shuts down, but since the
thread is basically aborted by the CLR, the final items that need to
be processed are not.

I have tried trapping the various events (AppDomain.DomainUnload,
etc), but the CLR usually pulls the rug out from under me.  I tried
finalizing, but as we know, the finalizer doesn't necessarily get
called.

What I need is basically to put my thread as a foreground thread, and
detect whether or not I am the "last thread standing" so I can do my
cleanup.

(We have a workaround requirement that an application must call a
"AppIsShuttingDown" function before it closes its last thread, but
I've noticed some of my colleagues are ignoring that requirement).
Vadym Stetsiak - 02 Oct 2007 23:00 GMT
Hello, Doug!

"Last thread standing" scenario is bad. Because application can have several
threads started by CLR and there is no guarantee that your thread will be
the last to stop.

Process class from System.Diagnostics has Threads property. Using this
property you can query for you thread.

But if we're talking about library code you can still handle
ThreadAbortException within your thread to do necessary cleanup. Anothew way
is to provide some close API for your library that is mandatory to call.
--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote  on Tue, 02 Oct 2007 15:45:59 -0000:

DS> Is there any way to detect/count the number of application defined
DS> foreground threads that are currently active?

DS> Background:  We have a library that processes application requests
DS> in the background via a queuing mechanism (basically the library has
DS> one thread that does the work, and the application's calls into the
DS> library just puts the item to be processed on a queue).  Before you
DS> say it, no MSMQ is not an option.  The problem is that there may be
DS> items remaining in the queue when the application shuts down...that
DS> need to be flushed.  If I make the worker thread a foreground
DS> thread, an application will never shut down completely because the
DS> CLR doesn't shutdown an app until all foreground threads have
DS> exited.  If I make it a background thread, and the application shuts
DS> down, but since the thread is basically aborted by the CLR, the
DS> final items that need to be processed are not.

DS> I have tried trapping the various events (AppDomain.DomainUnload,
DS> etc), but the CLR usually pulls the rug out from under me.  I tried
DS> finalizing, but as we know, the finalizer doesn't necessarily get
DS> called.

DS> What I need is basically to put my thread as a foreground thread,
DS> and detect whether or not I am the "last thread standing" so I can
DS> do my cleanup.

DS> (We have a workaround requirement that an application must call a
DS> "AppIsShuttingDown" function before it closes its last thread, but
DS> I've noticed some of my colleagues are ignoring that requirement).

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.