I haven't had any problems that I know of but could somebody look to see if I'm using the locks correctly? I want to make sure I'm not doing anything wrong before doing something similar to store a dataset that will be referenced on almost every page.
Public Class Config
Private Shared _SiteMessageLastCheck As Date
Private Shared _SiteMessage As String
Public Shared Property WebSiteMessage() As String
Get
' Only hit database if 60 seconds have passed since last time it was requested
If DateDiff(DateInterval.Second, _SiteMessageLastCheck, Now) > 60 Then
'Get message from database
Dim cls As New clsLoginData
cls.SysFunction = "WebSiteMessage"
cls.GetSysCtrlValue()
Dim objLock As Object = New Object()
Try
SyncLock objLock
_SiteMessage = cls.Command1
_SiteMessageLastCheck = Now
End SyncLock
Catch
_SiteMessage = ""
End Try
Return _SiteMessage
Else
Return _SiteMessage
End If
End Get
Set(ByVal value As String)
Try
'Update message in database
Dim cls As New clsLoginData
cls.SysFunction = "WebSiteMessage"
cls.Command1 = value
If Not cls.UpdateSysCtrlValue() Then ASPNETMsgBox("Failed to update Website Message.")
Dim objLock As Object = New Object()
Try
SyncLock objLock
_SiteMessage = value
_SiteMessageLastCheck = Now
End SyncLock
Catch
_SiteMessage = ""
End Try
Catch ex As Exception
MsgBox("Failed to update Website Message.")
End Try
End Set
End Property
End Class
Two requests hitting your method at the same time will get their own copy objLock so the lock will be on that object, so neither will be prevented by the other from entering the synchlock
Thread A gets Object A
Thread A locks Object A
Thread B gets Object B
Thread B locks Object B
No other threads are locking Object A so Thread A enters SyncLock
No other threads are locking Object B so Thread B enters SyncLock
You're going to have a bad time
Your lockObject needs to be shared and created at the class level
Public Class Config
Private Shared _SiteMessageLastCheck As Date
Private Shared _SiteMessage As String
Private Shared objLock As Object = New Object()
Public Shared Property WebSiteMessage() As String
Get
then just
Dim cls As New clsLoginData
cls.SysFunction = "WebSiteMessage"
cls.GetSysCtrlValue()
Try
SyncLock objLock
_SiteMessage = cls.Command1
_SiteMessageLastCheck = Now
End SyncLock
I'm afraid I no longer use this forum due to the new point allocation system.
Technically, yeah you could. That depends on how many methods and how often they are going to be called. I wouldn't be too bothered about sharing a lock betwen a couple of functions. Maybe not tens or hundreds though. Or if the methods are constantly
being called.
I'm afraid I no longer use this forum due to the new point allocation system.
Member
1 Points
5 Posts
SyncLock - Public Shared Property
Sep 04, 2013 07:53 PM|danp129|LINK
I haven't had any problems that I know of but could somebody look to see if I'm using the locks correctly? I want to make sure I'm not doing anything wrong before doing something similar to store a dataset that will be referenced on almost every page.
All-Star
37441 Points
9076 Posts
Re: SyncLock - Public Shared Property
Sep 04, 2013 08:02 PM|AidyF|LINK
Two requests hitting your method at the same time will get their own copy objLock so the lock will be on that object, so neither will be prevented by the other from entering the synchlock
Your lockObject needs to be shared and created at the class level
then just
Member
1 Points
5 Posts
Re: SyncLock - Public Shared Property
Sep 04, 2013 08:59 PM|danp129|LINK
Thanks, so do I need to use a different lock object for each Public Shared Method/Property I create?
All-Star
37441 Points
9076 Posts
Re: SyncLock - Public Shared Property
Sep 04, 2013 09:12 PM|AidyF|LINK
Technically, yeah you could. That depends on how many methods and how often they are going to be called. I wouldn't be too bothered about sharing a lock betwen a couple of functions. Maybe not tens or hundreds though. Or if the methods are constantly being called.
Member
1 Points
5 Posts
Re: SyncLock - Public Shared Property
Sep 04, 2013 09:19 PM|danp129|LINK
OK, thank you again.