Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Static Variable Life time Across Refresh and PostBack

Tags:

static

asp.net

WorkAround:

I have declared a class level Public static variable and initialized with a value 0 in the environment of ASP.NET 3.5 In load event I Incremented by 1 of that variable

Problem:

  1. After getting page refresh and even Postback, I am getting latest values of that variable. A variable declared as STATIC , not getting reset by Page refresh and Postback?
  2. I just close the browser and close the VS 2008 IDE - even though while i am reopen, rerun the same web application, I am getting last incremented value, Not 0. I am wondering how this is possible after i close application.

Could you please help on this.

like image 235
karthik Avatar asked Nov 11 '09 09:11

karthik


1 Answers

Static variables are valid for the entire AppDomain. When you close your browser you don't close the application as it continues to execute on the web server. Oh and forgot to mention: try to avoid using static variables in multi-threaded applications without proper locking mechanisms or you may run into race conditions.

like image 127
Darin Dimitrov Avatar answered Sep 21 '22 23:09

Darin Dimitrov