time static variable value is retained

G

Guest

Hi all,
I have written a small code in which i declare a static int variable and
increment its value by one each time i click on a button. actually i use the
int variable value in a particular URL so that i can browse to diff URL each
time i click the button. Hope you understand the scenario.
So here my question is how long is the value of the static variable
retained from the time i click on the button, because what i observe is when
the value of the in variable (static) goes from 1 to suppose 34 and there i
stop clicking and after may be 15 min i again click the button the value of
the int variable is again back to 1. So is there any way i can avoid this
from happening ? and what is the time limit the value of the variable is
retained.
here is the code.

static int cityID=1;
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Redirect("http://chicago.craigslist.org/cgi-bin/search?areaID=" +
cityID.ToString()
+"&subAreaID=0&query=entry+level%2C+asp.net%2C+c%23&cat=jjj",false);
cityID++;
}
 
N

Nicholas Paldino [.NET/C# MVP]

Helpseeker,

Static variables live for the life of the application domain. In
ASP.NET, the only reason the variable would reset is because the application
is resetting itself on the server side. This is most likely due to changes
which cause recompilation of the pages/assemblies.

If you can't control when the app is brought up/shut down, then you
might want to store this variable in a file, or some persistent medium, and
then fetch it when you need it.

Hope this helps.
 
G

Guest

Nicholas,
Actually the IIS server is on my local laptop itself. IIS is up an running
continuously, its not being shutdown. Yes storing in a file is always a
solution, but i was wondering why the variable value was resetting. Was just
curious to know why that was happening.
Thnks,
Helpseeker

Nicholas Paldino said:
Helpseeker,

Static variables live for the life of the application domain. In
ASP.NET, the only reason the variable would reset is because the application
is resetting itself on the server side. This is most likely due to changes
which cause recompilation of the pages/assemblies.

If you can't control when the app is brought up/shut down, then you
might want to store this variable in a file, or some persistent medium, and
then fetch it when you need it.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Helpseeker said:
Hi all,
I have written a small code in which i declare a static int variable and
increment its value by one each time i click on a button. actually i use
the
int variable value in a particular URL so that i can browse to diff URL
each
time i click the button. Hope you understand the scenario.
So here my question is how long is the value of the static variable
retained from the time i click on the button, because what i observe is
when
the value of the in variable (static) goes from 1 to suppose 34 and there
i
stop clicking and after may be 15 min i again click the button the value
of
the int variable is again back to 1. So is there any way i can avoid this
from happening ? and what is the time limit the value of the variable is
retained.
here is the code.

static int cityID=1;
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Redirect("http://chicago.craigslist.org/cgi-bin/search?areaID=" +
cityID.ToString()
+"&subAreaID=0&query=entry+level%2C+asp.net%2C+c%23&cat=jjj",false);
cityID++;
}
 
J

Jon Skeet [C# MVP]

Helpseeker said:
Actually the IIS server is on my local laptop itself. IIS is up an running
continuously, its not being shutdown. Yes storing in a file is always a
solution, but i was wondering why the variable value was resetting. Was just
curious to know why that was happening.

It's not a case of IIS shutting down completely, it's a case of the
AppDomain recycling, which it does automatically on occasion and when
you have new code.
 

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

Top