By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 2,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

Help Needed with Static Object Clean Up.

P: n/a
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



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.552 / Virus Database: 344 - Release Date: 12/15/2003
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

"Rick" <no****@nospam.com> wrote in message
news:e8*************@TK2MSFTNGP11.phx.gbl...
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



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.552 / Virus Database: 344 - Release Date: 12/15/2003

Nov 18 '05 #2

P: n/a
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.
"Nick" <fr**@here.there> wrote in message
news:ON**************@TK2MSFTNGP09.phx.gbl...
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.

"Rick" <no****@nospam.com> wrote in message
news:e8*************@TK2MSFTNGP11.phx.gbl...
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



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.552 / Virus Database: 344 - Release Date: 12/15/2003


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.558 / Virus Database: 350 - Release Date: 1/2/2004
Nov 18 '05 #3

P: n/a
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.
"Rick" <no****@nospam.com> wrote in message
news:eM**************@TK2MSFTNGP10.phx.gbl...
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.
"Nick" <fr**@here.there> wrote in message
news:ON**************@TK2MSFTNGP09.phx.gbl...
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.

"Rick" <no****@nospam.com> wrote in message
news:e8*************@TK2MSFTNGP11.phx.gbl...
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



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.552 / Virus Database: 344 - Release Date: 12/15/2003


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.558 / Virus Database: 350 - Release Date: 1/2/2004

Nov 18 '05 #4

P: n/a
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()
-----Original Message-----
OK. My suggestion would be to implement IDisposable, as thats a clean wayof telling the objects users that it can have resources that requireconsideration. Likewise, I could not see where the void Finalize code wasbeing 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 IDisposableInterface.

Nick.
"Rick" <no****@nospam.com> wrote in message
news:eM**************@TK2MSFTNGP10.phx.gbl...
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.

"Nick" <fr**@here.there> wrote in message
news:ON**************@TK2MSFTNGP09.phx.gbl...
> 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.
>
> "Rick" <no****@nospam.com> wrote in message
> news:e8*************@TK2MSFTNGP11.phx.gbl...
> > 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
> >
> >
> >
> >
> >
> >
> >
> > ---
> > Outgoing mail is certified Virus Free.
> > Checked by AVG anti-virus system (http://www.grisoft.com). > > Version: 6.0.552 / Virus Database: 344 - Release Date: 12/15/2003 > >
> >
>
>

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system

(http://www.grisoft.com). Version: 6.0.558 / Virus Database: 350 - Release Date: 1/2/2004

.

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.