Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, October 30, 2008 6:11 AM
Hi All,
I have used static variable in my Application to check the status of Logged in user, but I want to reset the value of static variable when I exit the application(Closing the application or clicking on Logout button). So how can I do this? Are any methods available to destruct the static variable on exiting the application?
Any help is appreciated.
Thanks in Advance,
Bhargavi.S
All replies (3)
Friday, October 31, 2008 7:00 AM âś…Answered
'static' does not work the way you think, and the term 'Application' is used differently in ASP.NET than the way you use it.
The Application life-time in ASP.NET spans any number of users and logins and logouts. The Application life time is long. Barring recycling, server restart and a fatal crash in the app it'll more or less run for ever. 'statics' will live just as long. They are shared by all users.
Your use of the static is not thread safe either.
Since you need a 'per user' state, you'll have to resort to a 'per user' persistence medium. In the web world the only one available is the client, and there this state is called a cookie. All per user state is cookie based, including ASP.NET session state (although only a key is stored in the cookie, the data is stored on the server).
Finally, what you really want to do is use the built-in forms authentication, the built-in membership providers, and the built-in controls that do all you want and more. Google 'forms authentication' for more info.
Thursday, October 30, 2008 10:20 AM
This is unnecessary.
All you are asking for is really a way to tell the garbage collector that the variable is available for collection. This is unnecessary when ending an application. It is also unnecessary for local variables just before ending a method. It is also unnecessary for instance variables when the containing object is destroyed. The only time you might want to signal to the garbage collector that you're finished with a variable is when the variable is in scope. When the variable goes out of scope - i.e., a static variable when the app exits, or an instance variable when the containing object is destroyed, or a local variable when a method ends - the garbage collector know about it.
Friday, October 31, 2008 1:32 AM
Firstly I want to Thank you for the response.
I accept with your explanation. But why I ve asked is :
public static int i = 0;
This is the variable I have used in my Application. And on Page_Load function i am calling,
if (i == 0)
{
Login(strUName, strPwd);
i++;
}
Because i want to call Login function only once, so I am using the above steps. And once it calls Login function i becomes 1. But it remains the same even when I exit my Application. And when I run second time, as i=1, it is not calling Login function. This is my problem.
I want to call Login function every time when I run the application.
And why I am using static is , I need the status of 'i' in other pages also, so I am using it as static.
Hope you got my requirement.
Thanks in Advance,
Bhargavi.S