Hi Val,
Even though you might close the application in the *normal way* finalizers
are not quarantee to be executed.
Following is a quote form the Jeffrey Richter'a article "Garbage Collection:
Automatic Memory Management in the Microsoft .NET Framework", which can be
found at
http://msdn.microsoft.com/msdnmag/issues/1100/gci/ - Part I and
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/ - Part II
"When an application terminates, some objects are still reachable and will
not have their Finalize method called. This can happen if background threads
are using the objects or if objects are created during application shutdown
or AppDomain unloading. In addition, by default, Finalize methods are not
called for unreachable objects when an application exits so that the
application may terminate quickly. Of course, all operating system resources
will be reclaimed, but any objects in the managed heap are not able to clean
up gracefully. You can change this default behavior by calling the System.GC
type's RequestFinalizeOnShutdown method. However, you should use this method
with care since calling it means that your type is controlling a policy for
the entire application."
I couldn't find the method RequestFinalizeOnShutdown, though.
What is not mentioned in the article, but can be found in the Jeffrey
Richter's book "Applied Microsoft.Net Framework Programming in... " is that
for every finalizer CLR gives 2 sec to terminate if a finalizer fails to
terminate in 2 sec the application is shuted down and no other finalizers
are called. For all finalizers CLR gives 40 sec to terminate and when this
time expires the application is shuted down and the rest of the finalizers
remain uncalled.
This time intervals are subject to change and this values are correct for
the time JR was writing the book.
Any ways the time intervals look big enough not to warry about them, but
there are still chances to have the finalizers not called. Anyway, since the
application is terminated and all umanaged resources will be reclaimed I
think the best is not to warry about them.
If the unmanaged resource is not one of the *windows unmanaged resources* (I
mean is some custom device which has to be shuted down explicitly) I think
you should be very carefull before using finalizers for that.
To finalize
![Wink ;) ;)](/styles/default/custom/smilies/wink.gif)
my post I just want to remind you that MS says the
programmers should avoid using finalizers *only* for cleaning the unmanaged
resources. IDisposable pattern should be used instead.
HTH
B\rgds
100