473,320 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Does static behave this way

375 256MB
Hi,

Kindly go through the code below
Expand|Select|Wrap|Line Numbers
  1. public partial class _Default : System.Web.UI.Page 
  2. {
  3.     public static int i = 0;
  4.     protected void Page_Load(object sender, EventArgs e)
  5.     {
  6.  
  7.     }
  8.     protected void Button1_Click(object sender, EventArgs e)
  9.     {
  10.         i = 5;
  11.         Response.Write(i.ToString());
  12.     }
  13.     protected void Button2_Click(object sender, EventArgs e)
  14.     {
  15.  
  16.  
  17.         Response.Write(i.ToString());
  18.     }
  19.     protected void Button3_Click(object sender, EventArgs e)
  20.     {
  21.  
  22.         Response.Write(i.ToString());
  23.     }
  24.     protected void Button4_Click(object sender, EventArgs e)
  25.     {
  26.  
  27.         Response.Redirect("default3.aspx");
  28.     }
  29.  
  30. }
  31.  
  32.  
Now if i Click the first button, the value of "i" will be 5, and then whether I click on button2 or button3 then also it will be displayed as 5.

Now I redirect to another page., and from there I return to the above page.
And I click button2 or button3, I get the value as 5 (should not be it 0)

Now I close the application and run, And I click button2 or button3, I get the value as 5

Can anyone please explain it.
I mean does static behave like session. and secondly when i close the application and run again(build+run) it shows the value 5.
Why is it so?



But if I initalise in page load event, it is showing properly. i.e. it does not show 5 when i click button 2 and button3


.Regards
cmrhema
Jul 13 '09 #1
13 1870
tlhintoq
3,525 Expert 2GB
Static basically means "create only one of this object and use it throughout". I'm sure someone else will elaborate on that, but you could get a more detailed understanding yourself by looking it up on MSDN. This is just the easy way to think of it.

i is a variable that has the scope of the entire class because it is not created within one of your methods. So of course when you set it to 5 with the first button click it remains that until the class is disposed of. Thus you see it's value of 5 with the next two methods.

So long as that class remains alive, the variable i remains alive.
Jul 13 '09 #2
cmrhema
375 256MB
Yes I do agree with that, But if we move to the next page or when I close the application, the class object will be disposed off(through the Garbage Collector, or am I wrong), then how does the value of the variable "i" persist.
Jul 14 '09 #3
tlhintoq
3,525 Expert 2GB
Garbage Collection is far from instantaneous.
If you want to force it you could .Dispose() of your class or you could call GC.Collect()
Jul 14 '09 #4
Bassem
344 100+
@cmrhema
Yes, if you closed the application, but you didn't.
As a ASP.NET website, when you close your browser, it doesn't mean you closed your application. You have to shut your server down, because your application is a server not a browser.
Look down left to your monitor, you will find that IIS opens a port to your application, to close it, just stop it.

That's what I thought.
Kind Regards,
Bassem
Jul 14 '09 #5
Frinavale
9,735 Expert Mod 8TB
Please take a look at this MSDN article on Static Classes and Static Class Members.

When you specify that a class member is "static" it means that this member does not belong to any particular instance of the class....it's "shared" and accessed by all instances of the class.

Your Page is a class. The variable "i" is static, which means that it's accessed by all instances of the Page class. An instance of the Page class is created when ever the user makes a request for the page. This means that if "i" is changed anywhere, it changes for all instances of this Page....

@Bassem
Even if you stop IIS your application doesn't shutdown...IIS just stops accepting incoming requests.


-Frinny
Jul 15 '09 #6
Bassem
344 100+
@Frinavale
I just tested it and I got what I respected, here the code I used for test:
Expand|Select|Wrap|Line Numbers
  1.     static int i;
  2.     protected void Page_Load(object sender, EventArgs e)
  3.     {
  4.         Label1.Text = i.ToString();
  5.     }
  6.     protected void Button1_Click(object sender, EventArgs e)
  7.     {
  8.         i = Convert.ToInt32(TextBox1.Text);
  9.         Label1.Text = i.ToString();
  10.     }
When I change i to 8 then close my browser and request the page again, it still 8.
But when I stop IIS, i became 0.
Do I miss something?

Thanks,
Bassem
Jul 15 '09 #7
Frinavale
9,735 Expert Mod 8TB
@Bassem
Really?!

What IIS version are you using?

I would love for this to happen for me because I have an web application that loads a COM object into memory. When I stop IIS the COM Object is still loaded in memory and people previously connected to the web app are still able to use it even though IIS is stopped.

This is especially annoying when people are trying to update my web application to a newer version...the update will fail if the COM Object is loaded in memory.

I was sure that stopping IIS only prevents people from making new connections....that it doesn't actually stop the web application from running.
Jul 15 '09 #8
Frinavale
9,735 Expert Mod 8TB
Hmmm....
According to this article:
Restarting or stopping IIS, or rebooting your Web server, is a severe action. When you restart the Internet service, all sessions connected to your Web server (including Internet, FTP, SMTP, and NNTP) are dropped. Any data held in Web applications is lost. All Internet sites are unavailable until Internet services are restarted.
However, when I stop IIS, the worker process is not killed and my COM Object remains loaded in memory...thus making it impossible to update my application without using task manager (or rebooting the machine) to kill the worker process.

I'm going to have to look into this further.

Thanks for pointing this out Bassem.
Jul 15 '09 #9
JamieHowarth0
533 Expert 512MB
Frinny,

Your problem is that when you try and stop IIS, any incoming requests at the moment that you stop it that make use of the COM object will immediately halt the "stopping" of IIS.
At a previous employer, they had huge problems upgrading a COM component of a website because every time they tried to stop the website (not IIS) and change the component, the component would be "locked" cause an incoming request would boot the COM component up and keep the website alive, contrary to the stop instruction.

Workaround:
1) Open up Component Services.
2) Navigate to your COM object in Component Services.
3) Drag your DLL from Windows Explorer into the Component Services window - you'll get a dialog box asking if you want to replace the existing component with the same name and a "Yes/No" dialog box.
3) Open up IIS.
4) Do this very very very quickly - right-click on the website, hit "Stop",
hit Alt+Tab to flick to Component Services, and hit Y.

You may have to do this a couple of times to get the timing perfect, however, it's the only way I know of upgrading a COM component that's used by IIS.

Hope it helps.

codegecko
Jul 16 '09 #10
Frinavale
9,735 Expert Mod 8TB
Thanks codegecko :)
That's pretty much what I have to do now...the problem is that the people who've bought the software also have to do it. I wish there was a better way but I tell them to do it in the middle of the night when there's less people using the application. Still, it's a problem.
Jul 16 '09 #11
JamieHowarth0
533 Expert 512MB
Well Frinny, unfortunately your only alternative is shutting down IIS entirely, then replacing the COM component - I'd say that the quick-window-flick-keyboard-shortcut-method is still the lesser of two evils :-)
Sep 2 '09 #12
Frinavale
9,735 Expert Mod 8TB
Hey cg, did you ever experience excruciatingly long load times when your web application was using a COM Object?

I can't stand this....every time I do a new build, and it's the first time loading a page in the website it takes a minimum of 23 seconds every time (I don't think you really understand how much time 23 seconds is.....and when I say minimum I mean minimum...sometimes taking upwards of 45 seconds and I just want to scream)

I'm assuming that the load time has something to do with loading the COM Object..but I don't know the actual reason. Whenever I try to run the ANTS profiler on it to try and examine why it's taking so long I manage to crash it (after contacting the company about the problem they said it's because of high latency time.......................no duh).

Any insight would be appreciated.
Sep 8 '09 #13
JamieHowarth0
533 Expert 512MB
Hey Frinny,

I've never had to deal with long load times on COM, if anything it's actually faster than most .NET libs I've dealt with cause it's assembly-compiled and subsequently runs faster, plus the apps I've dealt with then cache the COM object in IIS application pool memory for even faster load times.

If the vendor says it has a high latency time, is there any way you can refactor the COM's functionality to try and improve performance? I'm guessing it uses some form of network connectivity to cause such a high latency (allegedly), or it's just appallingly written :-D lol

codegecko
Sep 8 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Steven D'Aprano | last post by:
I've been doing a lot of reading about static methods in Python, and I'm not exactly sure what they are useful for or why they were introduced. Here is a typical description of them, this one...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
2
by: Pohihihi | last post by:
Actually I am not understanding what you mean by > attribute will not be applicable to the type, but rather, all types you mean it will behave like ?? Thanks, Po
4
by: Michael | last post by:
Hi, I have a class with static fields. The values of those fields appear to stick between Web requests. That behaviour is a bit of a surprise to me, as I would expect the object to be completely...
18
by: Jack | last post by:
Thanks.
10
by: sunil | last post by:
Hello, I am new to c# . I have some basic programming doubts. Please help me in clarifying these doubts. I want to initialize a static and readonly field with a value returned by a static...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
10
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

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.