473,395 Members | 1,558 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,395 software developers and data experts.

Windows service not releasing object

gb
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.
Nov 20 '05 #1
1 1433
"gb" <an*******@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
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

--
Sven Groot
Nov 20 '05 #2

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

Similar topics

1
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. I'm having trouble getting the code that I've written to work, can anyone shed some light as to where I'm...
0
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. The program I'm trying to develop needs to be able to do the following: - Select remote server -...
0
by: gb | last post by:
I have a Windows Service program that monitors a directory. Once a file is added(created) to the directory, it calls code to process that file. Below is a summarized version of the code 1. ...
4
by: WinDev | last post by:
We are trying to build a system where we have a Windows Service do some manipulation of data, and then sending the data to a Windows App. I had posted a question of how we should do this and was...
2
by: Trevor | last post by:
Argh! This problem is driving me nuts! Can you help? In November of 2003, I installed a web service on Windows Server 2003 built in VB.NET for v1.1.4322 of the framework. It contains a timer...
2
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
7
by: HeatherS | last post by:
We are having issues with our windows services using memory and never releasing it. We have one service that has a file watcher which takes an xml file, inserts some records into a database, and...
4
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to...
10
by: tshad | last post by:
I have a Windows Service that I need to put to sleep for about 10-20 minutes. The problem is that if you try to shut it down during this time, it will tell you that the service didn't respond in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.