Windows service not releasing object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've written a Windows Service program that monitors a directory using the FileSystemWatcher class. The monitoring is done on it's own thread. Once a particular files gets added(created) to the directory, the program calls a routine using ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf module.sub)). That routine creates an object of another class. The problem is that classes' Finalize method is not called until the Windows Service is stopped. Does anybody know why that object is not released until the service is stopped and a way for me to ensure that classes' Finalize method gets called

Here is a summerized version of the code

1. The OnStart of the service creates a reference to the monitor class and creates a thread for the main routine of the monitor class to run on
2. The main routine of the monitor class creates a FileSystemWatcher object and performs a do loop waiting for a file to get created using WaitForChanged(WatcherChangeTypes.Created
3. Once a file is added to the directory, the ThreadPool is used to call a routine in another module using ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf module.subroutine)
4. That routine creates a reference to let's say X class

The problem is that X classes' Finalize method never gets called until the Windows Service is stopped. I hope this is somewhat clear and makes sense to someone. Any help is appreciated.
 
gb said:
I've written a Windows Service program that monitors a directory using the
FileSystemWatcher class. The monitoring is done on it's own thread. Once
a particular files gets added(created) to the directory, the program calls
a routine using ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf
module.sub)). That routine creates an object of another class. The
problem is that classes' Finalize method is not called until the Windows
Service is stopped. Does anybody know why that object is not released
until the service is stopped and a way for me to ensure that classes'
Finalize method gets called?

Here is a summerized version of the code:

1. The OnStart of the service creates a reference to the monitor class
and creates a thread for the main routine of the monitor class to run on.
2. The main routine of the monitor class creates a FileSystemWatcher
object and performs a do loop waiting for a file to get created using
WaitForChanged(WatcherChangeTypes.Created)
3. Once a file is added to the directory, the ThreadPool is used to call
a routine in another module using ThreadPool.QueueUserWorkItem(New
WaitCallback(AddressOf module.subroutine))
4. That routine creates a reference to let's say X class.

The problem is that X classes' Finalize method never gets called until the
Windows Service is stopped. I hope this is somewhat clear and makes sense
to someone. Any help is appreciated.

A class' Finalize method will not be called until the garbage collector runs
and decides to collect the object. There is no deterministic way to
determine when this will happen. Although it's possible to invoke the GC
manually, this is not a good idea. What you want to do is add a Dispose (or
Close if that makes more sense for the type of unmanaged resource your class
is representing) method to the class and call that yourself when you need
the cleanup code to run. Don't forget to call GC.SuppressFinalize from your
Dispose method and make sure the class can't be used anymore after calling
Dispose (methods of a disposed object must throw an
ObjectDisposedException).

Look up the IDisposable interface in the help.
This article could also be useful:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcn7/html/vaconUsingConstructorsDestructors.htm
 
Back
Top