Does static behave this way

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    Does static behave this way

    Hi,

    Kindly go through the code below
    Code:
    public partial class _Default : System.Web.UI.Page 
    {
        public static int i = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            i = 5;
            Response.Write(i.ToString());
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
          
    
            Response.Write(i.ToString());
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
           
            Response.Write(i.ToString());
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
           
            Response.Redirect("default3.aspx");
        }
    
    }
    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
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.

    Comment

    • cmrhema
      Contributor
      • Jan 2007
      • 375

      #3
      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.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Garbage Collection is far from instantaneous.
        If you want to force it you could .Dispose() of your class or you could call GC.Collect()

        Comment

        • Bassem
          Contributor
          • Dec 2008
          • 344

          #5
          Originally posted by cmrhema
          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.
          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

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            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....

            Originally posted by Bassem
            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.
            Even if you stop IIS your application doesn't shutdown...IIS just stops accepting incoming requests.


            -Frinny

            Comment

            • Bassem
              Contributor
              • Dec 2008
              • 344

              #7
              Originally posted by Frinavale
              Even if you stop IIS your application doesn't shutdown...IIS just stops accepting incoming requests.
              I just tested it and I got what I respected, here the code I used for test:
              Code:
                  static int i;
                  protected void Page_Load(object sender, EventArgs e)
                  {
                      Label1.Text = i.ToString();
                  }
                  protected void Button1_Click(object sender, EventArgs e)
                  {
                      i = Convert.ToInt32(TextBox1.Text);
                      Label1.Text = i.ToString();
                  }
              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
              Last edited by Bassem; Jul 15 '09, 03:15 PM. Reason: correct wrong end tag

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by Bassem
                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?
                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.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  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.

                  Comment

                  • JamieHowarth0
                    Recognized Expert Contributor
                    • May 2007
                    • 537

                    #10
                    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

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      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.

                      Comment

                      • JamieHowarth0
                        Recognized Expert Contributor
                        • May 2007
                        • 537

                        #12
                        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 :-)

                        Comment

                        • Frinavale
                          Recognized Expert Expert
                          • Oct 2006
                          • 9749

                          #13
                          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...somet imes 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.

                          Comment

                          • JamieHowarth0
                            Recognized Expert Contributor
                            • May 2007
                            • 537

                            #14
                            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

                            Comment

                            Working...