afilbert
afilbert

Reputation: 1540

Public Properties within VB Module - Cross-client Behavior

Can one client call a public property within a VB.NET module and see the value of that public property changed by another client accessing it at the same time?

Example:

Client 1 calls

Public Module DataModule
    Private theDateTime As DateTime = GetAdjustedDateTime() //initial TZ value

    Public Property GetSetDateTime() As DateTime
        Get
            Return theDateTime
        End Get
        Set(ByVal value As String)
            theDateTime = value
        End Set
    End Property
End Module

by first setting the Property and then getting the value throughout WhateverMethod()...

Partial Class admintours
    Inherits System.Web.UI.Page

    Private Sub WhateverMethod()
            GetSetDateTime = Now
            ...
            ...
            ... //code
            ...
            SomeFunction(GetSetDateTime) //value is 10/14/2010 00:23:56
            ...    
            ...
            //almost simultaneously Client 2 sets the value to Now.AddDays(-1) 
            ...
            SomeOtherFunc(GetSetDateTime) //value passed in: 10/13/2010 00:23:56
            ...
            ...
            ... //some more code
            ...
    End Sub
End Class

I'm running into random instances where it looks like another client might be modifying (by setting) the value of GetSetDateTime DURING the first client's run through of WhateverMethod(). This is alarming to me and I've been trying to figure out if that's a possibility. Any confirmation or otherwise as to that would be helpful, thanks!

Upvotes: 4

Views: 3661

Answers (2)

Enigmativity
Enigmativity

Reputation: 117084

Yes, if by "client" you mean separate threads in a single application (also assuming a single CPU process and a single AppDomain).

Now, you suggest that it is "alarming" if this is a possibility, so I assume that you want to make sure that this doesn't happen? In order words, you want to ensure that the value of GetSetDateTime remains unchanged during the execution of WhateverMethod.

It sounds like WhateverMethod is only run by "client 1", and the "client 2" code that changes the GetSetDateTime property is independent of WhateverMethod. It doesn't sound like SyncLock would help here.

If both clients are able to change GetSetDateTime at any time then you need to modify WhateverMethod like so:

Private Sub WhateverMethod()
        Dim localNow = Now
        GetSetDateTime = localNow
        ...
        SomeFunction(localNow)
        ...    
        SomeOtherFunc(localNow)
        ...
End Sub

Does this help?

Upvotes: 1

JaredPar
JaredPar

Reputation: 754953

Modules in VB.Net are shared within an AppDomain. So two clients within the same AppDomain will be operating on the same instance of any given Module. This means one could easily see the results of the other writing to the Module if they are running in parallel in the same AppDomain

In many ways it's best to view the data stored in a Module as global (it's not truly global but behaves that way for many samples).

Upvotes: 2

Related Questions