Hi Cj,
Finally when that sub is finished how can I clean up or dispose of that instance I
created when I started the sub
What do you mean by 'clearn up or dispose' in the above sentence, destroy
the instance, or just release the resource that the instance uses but not
managed by CLR, e.g. Windows handles and database connections?
In .NET world, it's the garbage collector's responsibility to release and
destroy an object. In fact, we have no way to destroy an object manully or
actively in .NET world at all. After an object leaves scope, it is ready
to be destroyed by the garbage collector.
Before releasing object, the CLR automatically calls the Finalize method
for objects that define a Sub Finalize procedure. The Finalize method can
contain code that needs to execute just before an object is destroyed, such
as code for closing files and saving state information. There is a slilght
performance panalty for executing Sub Finalize, so you should define a Sub
Finalize method only when you need to release objects explicitly.
Objects are released more quickly when system resources are in short suppy,
and less frequently otherwise, which means that you cannot determine
exactly when the object will be destroyed.
To supplement garbage collection, your class can provide a mechanism to
actively manage system resources if they implement IDisposable interface.
IDisposable has one method, Dispose, which clients should call when they
finish using an object. You can use the Dispose method to immediately
release resources and perform tasks as closing files and database
connections.
In your paractice, if the referenced object uses some resource and you want
to release them, you could write code in the overrides Finalize method. At
the end of the function, you need to set the variable x to Nothing in order
to make the referenced object out of scope. Then the text step is to wait
for garbage collector to release and destroy the object. If you'd like to
release the resource the referenced object uses immediately, you could
implement IDisposable interface in your class and call the Dispose method
of the referenced object at the end of the function.
For more information on how objects are created and destroyed and how to
implement IDisposable interface, you may visit the following link.
" Object Lifetime: How Objects Are Created and Destroyed"
http://msdn2.microsoft.com/en-US/library/hks5e2k6.aspx
"Using Constructors and Destructors"
http://msdn2.microsoft.com/en-us/library/2z08e49e.aspx
Hope I made some clarification.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.