Newbie: desctructor/dispose/garbage collection

J

Jeff

IDE: VS .NET 2003
OS: XP pro sp2

I've got a class (class A) which holds reference to n other classes (class
B). The reference from class A to class B is contained in an ArrayList in
class A..

public class A
{
private ArrayList array;
public class A()
{
array = new ArrayList();
}
}

public class B
{
public class B()
{
}
}

class A have got a thread which starts once every minutte, and remove
objects (class B) from the array based on some kind of criteria... (idle)...
Class B contains only .NET (managed) code, and contains also data which I
need saved back to the database when the object is destroyed

I've read this topic on MSDN:
http://msdn.microsoft.com/library/d...l/frlrfSystemIDisposableClassDisposeTopic.asp

My questions:
1)
The link above, mention what to do if I've got a class containing unmanged
code, but my classes contains only managed code. Can I use this example
then?

2)
If I can use the info at the link, where in class B should I place the code
that saves data back to the database (it's a stored procedure)... in public
void Dispose(), private void Dispose(bool disposing) or destructor

3)
If I should place the code in public void Dispose() I would be thankful for
info about where in this metod should I place the call to the stored
procedure (see below):
In public void Dispose()
Should I call the stored procedure between the line "Dispose(true);"
and line "GC.SuppressFinalize(this);"??



Jeff
 
E

Eyal Safran

Hello,

I would place your code in the:
protected virtual void Dispose(bool disposing)
{
// your code here
}

for the mere reason, that both Dispose() and Finalize() call this
method, and in both cases:
1) your instance of Class B lost all references, and so the GC called
the Finalize() which will call the Dispose(false)
2) you intentionally called the Dispose() on your object.
you would like to save the data (don't you???).

Anyone has any other ideas? I also would like to hear them...

Eyal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top