|
-
Mar 22nd, 2011, 11:45 AM
#1
Thread Starter
Fanatic Member
Destructor /Terminate
In vb6 we have terminate events. Code that must be called when object is destroyed.
Do we have that on vb.net?
-
Mar 22nd, 2011, 12:35 PM
#2
Re: Destructor /Terminate
you could use the Form_Disposed event:
vb Code:
Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
'...
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 22nd, 2011, 12:53 PM
#3
Re: Destructor /Terminate
The most direct equivalent is Finalize() I suppose (because it will be called no matter what when the object is destroyed), but never use it without Dispose() (and you won't even need Dispose() much either) unless you have a good reason:
Code:
Protected Overrides Sub Finalize()
Try
'Clean up your unmanaged resources here.
Finally
'Base cleanup
MyBase.Finalize()
End Try
End Sub
Events are usually a much better option. What are you trying to do here?
Last edited by minitech; Mar 22nd, 2011 at 12:58 PM.
-
Mar 22nd, 2011, 12:55 PM
#4
Re: Destructor /Terminate
It depends on the object. Some objects raise the Disposed event (those almost always inherit the Component class or implement the IDisposable interface somehow) and most don't.
If an object does raise the Disposed event then you can handle that event (as in Paul's example for a Form). If an object doesn't have that event - that's most likely because it's not needed - you can always subclass it and raise your own Disposed event.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Mar 22nd, 2011, 01:28 PM
#5
Re: Destructor /Terminate
It's a complicated and difficult topic in .NET.
.NET's memory is automatically cleaned up by an entity called the Garbage Collector (GC). For the most part, you don't know when the GC runs and you don't care. If an object needs to release resources in a hurry, it has to implement that mechanism itself.
There's one hook for the GC. If an object implements a special method called a finalizer, the GC executes it as part of cleanup. You can implement a finalizer on a class in VB .NET by declaring a Sub Finalize(), but there's several reasons why a user might not want to implement a finalizer.
There is a pattern called the Dispose pattern that .NET follows that doesn't have the disadvantages of finalizers. Unfortunately it's really difficult to get it right and it's a convention, not something built in to .NET. Here's an MSDN example, but you really have to read several to get a feel for it. I'll skip implementation detals since you didn't ask how to implement it.
A class that implements the pattern may choose to raise an event when it is disposed, but it's not required; it's not even part of most tutorials on the pattern.
So the answer in general is no, .NET has no guaranteed mechanism for getting a notification when an object is removed from memory. But there are mechanisms objects may implement to know when they are being destroyed. If they implement these mechanisms, they may provide a way for you to know. But it's not universal.
-
Mar 22nd, 2011, 03:21 PM
#6
Re: Destructor /Terminate
If you have an object that needs disposing, inherit from the IDisposable interface. When you press the <enter> key after typing 'Inherits IDisposable' it will add the necessary methods to your form automatically.
You will generally need to do this if your object creates, for example, objects which themselves need disposing, for example, Graphics.Pen object.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Mar 22nd, 2011, 03:38 PM
#7
Re: Destructor /Terminate
 Originally Posted by SJWhiteley
If you have an object that needs disposing, inherit from the IDisposable interface. When you press the <enter> key after typing 'Inherits IDisposable' it will add the necessary methods to your form automatically.
You will generally need to do this if your object creates, for example, objects which themselves need disposing, for example, Graphics.Pen object.
It should be "Implements" for an interface
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Mar 22nd, 2011, 08:48 PM
#8
Thread Starter
Fanatic Member
Re: Destructor /Terminate
I see. The reason I asked is webclient objects tend to have problems after I run the program for 10 minutes. I wonder if there is some form of memory leak.
Memory leak is something that won't happen on vb.net right?
-
Mar 22nd, 2011, 08:49 PM
#9
Re: Destructor /Terminate
Not usually, no. But your problem (from your other thread) most likely does not have anything to do with memory leaks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|