Is anyone else using Public Modules with Public Variable in aspx

S

Steve Mauldin

Having weird things happening with my code. Two users on at the same time
and data entered by one user is added into another users global variable.
global variable data being stored in session variable between pages. any
ideas?
 
G

Guest

I have used the following to avoid contention...


' The following is used to synchronize access to the global variable
SyncLock GetType(Global)
Global.TotalLogins = Global.TotalLogins + 1
End SyncLock
 
B

Bruce Barker

in a module, both private and public fields are shared between all threads
(concurrent requests), because all module fields (variables not defined
inside a function/sub) are shared by default. variables defined in
functions/subs are on the local stack, so they are not shared.

you could use attributes to make them thread local storage (like vb6), but
then you'd have to guarantee an asp.net request was handled by a single
thread (not the default) at a performance loss.

-- bruce (sqlwork.com)
 
S

Steve Mauldin

What about variables defined in a class?
as in Current Account in the example below
Public Class CheckoutCustomization

Inherits UIX.Interior_NoUC

Private CurrentAccount As Account

.........

end class
 
S

Steve Mauldin

Followup. It is my recommendation to anyone developing ASP.NET 1.1 not to
use Publuc Variables in Public Modules and specifically not to use them in
conjunction with session variables. Data persists from on persons session
to anothers and I had sessions overwriting each others data. The code to
duplicate this persistence is quite simple and is attached below (Special
Thanks to KJ for posting the code to duplicate). An interesting side point
is if you were wanting to share data between different users then this would
be a quick and fast way of doing it at the application level since you would
not have to redim or reload the data before using it. Everyone is pointing
to the same point in memory and the pointer stays across sessions. I can
see a use for it in gaming environments already. :)




*** code follows ***
' Module code

Module TestModule1
Public GlobalSettingsInstance As New GlobalSettings
End Module

Public Class GlobalSettings
Public Int1 As Int32 = Int32.MinValue
End Class

'aspx #1

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
GlobalSettingsInstance.Int1 = 567
'Session("GSI") = GlobalSettingsInstance 'tried with this commented
and uncommented
End Sub

Private Sub lbtnGoToPage2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles lbtnGoToPage2.Click
Response.Redirect("TestPage2.aspx", False)
End Sub

'aspx #2
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Write("Hello World") 'breakpoint here reveals .Int1 member =
567, regardless of session call in aspx #1
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top