Cluskitt
asked on
Global variables in .Net with multi-users are getting switched.
We have a .Net web application. Due to many reasons, I created a Public Class in App_Code folder. It has, among others:
Now, I was under the impression that global class variables were stored pretty much like session variables, that is, they are linked to the username/computer. A user in one computer would have totally separate variables from those of another user in another computer. I decided to use this approach because these are short term variables (usually used within the next minute or two and then become useless) and I already had the class created for other methods. But if this isn't reliable, then I'd have to switch to keeping these values in a table and connecting to the database all the time, which I'd rather not do. Session variables would share the same problems, despite being shorter lived (we've tried to make them stay alive for more than 20 minutes, but utterly failed, despite trying various different solutions).
So, does anyone know why this is happening? Is this to be expected from using global variables? Or is this just some freak problem? Can it be solved via some settings, on .Net or IIS? Will I have to move these variables to a table in the DB? What should be the best approach?
EDIT: It should also be noted that this particular user kept having wrong values returned for some time (to my knowledge no other changes occured within seconds of it), then changed computer and, apparently, things worked fine.
Private Shared _AltPopNum As Integer
Public Shared Property AltPopNum() As Integer
Get
Return _AltPopNum
End Get
Set(value As Integer)
_AltPopNum = value
End Set
End Property
Apparently, everything worked fine. A couple pages set this value, which can then be accessed in another couple pages. But recently, a case occured where this value was switched between users. That is, UserA inserted the value 10. Two seconds later, UserB inserted the value 15. When UserA read the variable, it was assigned as 15.Now, I was under the impression that global class variables were stored pretty much like session variables, that is, they are linked to the username/computer. A user in one computer would have totally separate variables from those of another user in another computer. I decided to use this approach because these are short term variables (usually used within the next minute or two and then become useless) and I already had the class created for other methods. But if this isn't reliable, then I'd have to switch to keeping these values in a table and connecting to the database all the time, which I'd rather not do. Session variables would share the same problems, despite being shorter lived (we've tried to make them stay alive for more than 20 minutes, but utterly failed, despite trying various different solutions).
So, does anyone know why this is happening? Is this to be expected from using global variables? Or is this just some freak problem? Can it be solved via some settings, on .Net or IIS? Will I have to move these variables to a table in the DB? What should be the best approach?
EDIT: It should also be noted that this particular user kept having wrong values returned for some time (to my knowledge no other changes occured within seconds of it), then changed computer and, apparently, things worked fine.
ASKER
Does that apply to a web application? There aren't multiple threads, just multiple sessions on different computers. Maybe I'm just being a bit thick (I'm fairly decent at desktop programming, but I hate web dev), but it seems to me that it applies only to a desktop app?
yes it does apply to web, as each request i handled in separate thread....
Just synchronize the shared variable and see the effect....
Read here:-
http://technicalsol.blogspot.in/2009/04/aspnet-static-variable.html
Summary : Either get rid of them(Preferred) or synchronize them
Just synchronize the shared variable and see the effect....
Read here:-
http://technicalsol.blogspot.in/2009/04/aspnet-static-variable.html
Summary : Either get rid of them(Preferred) or synchronize them
ASKER
Ok. This, however, seems to apply for multiple instances on the same computer. Which makes sense, because it would share the same session/cookies/etc. But my problem happened with two different computers, on two different locations. Shouldn't those be handled separately? Or does the ASP.Net engine simply consider any connection as a thread?
I could fix this easily with url redirection, but I don't wish to have any data on the URL where it can be manipulated.
Also, it seems to me that the purpose of the first link you provided is to prevent access to variables while they're being written. And that isn't the problem I'm having. The problem I have is that, using my above example, UserA should have a value of 10, even though UserB has a value of 15 for the same variable. In fact, UserA should have a value of 10 even if 50 different users assign a value to their variable. Each variable should be user independent.
I could fix this easily with url redirection, but I don't wish to have any data on the URL where it can be manipulated.
Also, it seems to me that the purpose of the first link you provided is to prevent access to variables while they're being written. And that isn't the problem I'm having. The problem I have is that, using my above example, UserA should have a value of 10, even though UserB has a value of 15 for the same variable. In fact, UserA should have a value of 10 even if 50 different users assign a value to their variable. Each variable should be user independent.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So, the only solution for keeping variables User Independent without using session (or url redirection), would be DB based, right? Luckily, we don't use those for that many pages, so I won't have to replace much code, but we do use those pages quite a bit. I was hoping for a way to store them locally without session variables, due to the difficulties in maintaining their persistence.
another way is to store them on client in some hidden text boxes...
If your data is relatively lesser, then try for this approach...
If your data is relatively lesser, then try for this approach...
ASKER
It is lesser, but it has to carry across pages, which is why I used the global variables route. Basically, half a dozen variables are set in one page, which then have to be accessed by another different page, which uses those variables for some parametrization.
use cookies then....
ASKER
Those are pretty much the same as session variables and can be tampered with like url redirection. I guess I will have to use DB based variables. It will be a small table anyway. Even if we have about 100 users, we won't have more than 2000 records at any time. With the added bonus that variables will carry over different sessions, which might actually have its uses. Thanks for your help. I'm going to close the question now.
ASKER
Thanks for the help.
This can be done using Monitor.Enter and Monitor.Exit Methods or SyncLock VB Statement
Here is a tutorial to help you on that:-
http://www.developerfusion.com/article/5184/multithreading-in-vbnet/2/