473,583 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help Needed with Static Object Clean Up.

Hello,

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

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

private void Page_Load(objec t sender, System.EventArg s e)
{
GlobalCounter.I ncrement();
}

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.Elap sed += new ElapsedEventHan dler(RecordCoun ter);
basicTimer.Inte rval = 60000;
basicTimer.Enab led = 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.Enab led = false;
basicTimer.Disp ose();
}

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
4 1795
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******** *****@TK2MSFTNG P11.phx.gbl...
Hello,

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

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

private void Page_Load(objec t sender, System.EventArg s e)
{
GlobalCounter.I ncrement();
}

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.Elap sed += new ElapsedEventHan dler(RecordCoun ter);
basicTimer.Inte rval = 60000;
basicTimer.Enab led = 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.Enab led = false;
basicTimer.Disp ose();
}

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
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.ther e> wrote in message
news:ON******** ******@TK2MSFTN GP09.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******** *****@TK2MSFTNG P11.phx.gbl...
Hello,

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

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

private void Page_Load(objec t sender, System.EventArg s e)
{
GlobalCounter.I ncrement();
}

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.Elap sed += new ElapsedEventHan dler(RecordCoun ter);
basicTimer.Inte rval = 60000;
basicTimer.Enab led = 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.Enab led = false;
basicTimer.Disp ose();
}

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
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******** ******@TK2MSFTN GP10.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.ther e> wrote in message
news:ON******** ******@TK2MSFTN GP09.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******** *****@TK2MSFTNG P11.phx.gbl...
Hello,

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

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

private void Page_Load(objec t sender, System.EventArg s e)
{
GlobalCounter.I ncrement();
}

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.Elap sed += new ElapsedEventHan dler(RecordCoun ter);
basicTimer.Inte rval = 60000;
basicTimer.Enab led = 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.Enab led = false;
basicTimer.Disp ose();
}

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
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.SuppressFina lization() 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 requireconsideratio n. 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******* *******@TK2MSFT NGP10.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.ther e> wrote in message
news:ON******** ******@TK2MSFTN GP09.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******** *****@TK2MSFTNG P11.phx.gbl...
> > Hello,
> >
> > Below I create a static instance of an object, works well. > >
> > public static BasicCounter GlobalCounter = new
> > BasicCounter("G lobalCounter");
> >
> > private void Page_Load(objec t sender, System.EventArg s e) > > {
> > GlobalCounter.I ncrement();
> > }
> >
> > 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.Elap sed += new ElapsedEventHan dler (RecordCounter) ; > > basicTimer.Inte rval = 60000;
> > basicTimer.Enab led = 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.Enab led = false;
> > basicTimer.Disp ose();
> > }
> >
> > 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1997
by: Mike | last post by:
Please help this is driving me nuts. I have 2 forms, 1 user class and I am trying to implement a singleton class. Form 1 should create a user object and populate some properties in user. Form2 should then have access to those user properties. I am not getting the property value from user that was set in form1. Sorry for posting so much...
33
3330
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
2
1934
by: MyNameIsnt | last post by:
Can anyone tell me why, when I click on the buttons it register 2 characters on the display? if you use the right mousebutton it works ok, but the buttons dont flash?? it works fine without the aqua buttons, (using just windows forms buttons... Main Form cs file...
0
1882
by: john | last post by:
Hi,All MS Tech Gurus: I need your help!!! It is the second time I post this message, I need get some feedback ASAP, Please Help!! Thanks a lot in advance. John I have a csharp method, using emit to dynamically generate classes & method depends on the meta table in the database,here is my situation 1) In One method I generated the code...
4
1956
by: Doruk | last post by:
The problem that we are experiencing is simple: We want to pass certain parameters from a page with several server controls to another page. We want to do this in a dotnet compliant manner, but we can't seem to find a good and clean solution anywhere. The options we looked into are as follows. Comments following the options are why we...
1
1691
by: Sky Sigal | last post by:
(PS: Cross post from microsoft.pulic.dotnet.framework.aspnet.webcontrols) I've been looking lately for a way to keep the Properties panel for Controls 'clean'... My goal is to keep similar public properties of a custom Control neatly tied together -- rather than all over the IDE. One such set of values that will rarely be changed, so...
12
2561
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in advance, Joe
11
2103
by: Bryan Kyle | last post by:
Hi All, I'm fairly new to C# and Generics and I'm wondering if anyone has some suggestions for me. I'm trying to implement a simple DAO framework using generics to keep my code as clean as I can, however I'm getting an error with what seems to me to be correct code. The error I'm getting is: Error 1 Cannot implicitly convert type...
3
1927
by: TC | last post by:
Hey All, I have some classes that I recently built for a website which uses the HttpWebRequest & HttpWebResponse objects from the System.Net namespace. Basically, the classes rap submitted data up, connect to external websites on external servers and post / remove the data from these other sites. It works fine locally but when uploaded to...
0
7888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7811
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8314
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7922
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5689
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.