Help Needed with Static Object Clean Up.

  • Thread starter Thread starter Rick
  • Start date Start date
R

Rick

Hello,

Below I create a static instance of an object, works well.

public static BasicCounter GlobalCounter = new
BasicCounter("GlobalCounter");

private void Page_Load(object sender, System.EventArgs e)
{
GlobalCounter.Increment();
}

In BasicCounter the counts are stored in a Hashtable and I use the following
code to fire a method to store them in the database, once again all is
working well.

basicTimer.Elapsed += new ElapsedEventHandler(RecordCounter);
basicTimer.Interval = 60000;
basicTimer.Enabled = true;


In BasicCounter, I need to stop the timer when an instance of this object is
"unloaded". Meaning, when I recompile my page from VS.net, the counter
continues to fire and I don't want it to, this code seems to be working.
void Finalize()
{
/* Stop basicTimer, and then kill it */
basicTimer.Enabled = false;
basicTimer.Dispose();
}

Here's where it gets tricky, when I add these two method calls Finalize(),
they don't work, I even wait fifteen minutes for garbage collection to get
to it on localhost, still nothing:
// Write "Hello World" to a txt file..
// Call method that records Hashtable to DB method.

How can I be sure that if a static object instance is being unloaded, i.e.
page compile, that the current counts get recorded. Do objects have a built
in Application_End method?

Thank you in advance.

Rick
 
Rick,

Maybe you could consider a more simple design for storing the counters.
Could you record the counters in the DB when a session ends?

Nick.
 
Sessions will not be used. The particularities of how to get this app to
work best aside, I'm most interested in how to do this type of clean up.
 
OK. My suggestion would be to implement IDisposable, as thats a clean way
of telling the objects users that it can have resources that require
consideration. Likewise, I could not see where the void Finalize code was
being invoked from. An object defines a destructor as:

public class Foo
{
public Foo(/* constructor */)
{
}

~Foo()
{
}
}

But I wouldnt put my code in the destructor, I would use the IDisposable
Interface.

Nick.
 
Hi Rick,

I believe Nick's suggestion to implement the IDisposable
interface to have deterministic finalization is the best
approach. I can forward you a generic way to apply this to
all of your objects.

An important note to remember here is that if you supply a
destructor in C#, the compiler emits a Finalize method.
The garbage collector promotes all objects (that it
considers garbage) that expose a Finalize method to a
finalization list during the first pass of garbage
collection. Therefore, your object is, in a sense,
ressurected upon first pass so that the framework can call
the finalize method on it. This could be why your object
is not completely dead.

Anyway... with all that said, you may want to implement
IDisposable and in the Dispose() method call
GC.SuppressFinalization() after stopping the time which
will immediately release all resources and release the
memory consumed by your object-your object will essetially
be dead at this point.

Hope this helps.



-IDoNothing.Go()
 
Back
Top