का उपयोग करने का उचित तरीका यह दो सूची (टी) ऑब्जेक्ट्स को लॉक करने के संबंध में previous question पर एक फॉलो-अप है। जवाब वहां सहायक था लेकिन मुझे एक और सवाल के साथ छोड़ दिया।सिंकलॉक (सामान्य रूप से)
मान लीजिए मैं इस तरह एक समारोह है:
Public Function ListWork() As Integer
List1.Clear()
..Some other work which does not modify List1..
List1.AddRange(SomeArray)
..Some more work that does not involve List1..
Retrun List1.Count
End Function
जो एक वर्ग है कि List1 वाणी में रहता है। एक बहुप्रचारित वातावरण में, अब मैं समझता हूं कि मेरे पास सूची 1 के लिए एक निजी लॉकिंग ऑब्जेक्ट होना चाहिए और जब भी यह संशोधित या गणना की जाती है तो सूची 1 लॉक करें। मेरा प्रश्न है, मैं इस करना चाहिए:
Private List1Lock As New Object
Public Function ListWork() As Integer
SyncLock List1Lock
List1.Clear()
..Some other work which does not modify List1..
List1.AddRange(SomeArray)
..Some more work that does not involve List1..
Dim list1Count As Integer = List1.Count
End SyncLock
Retrun list1Count
End Function
मेरा अनुमान है कि यह है कि पूर्व उदाहरण इष्टतम है:
Private List1Lock As New Object
Public Function ListWork() As Integer
SyncLock List1Lock
List1.Clear()
End SyncLock
..Some other work which does not modify List1..
SyncLock List1Lock
List1.AddRange(SomeArray)
End SyncLock
..Some more work that does not involve List1..
SyncLock List1Lock
Dim list1Count As Integer = List1.Count
End SyncLock
Retrun list1Count
End Function
या है?
मुझे लगता है कि मैं इसे अब समझता हूं। बहुत सूक्ष्म। मुझे कक्षा या कुछ लेना चाहिए ... अंतर्दृष्टि के लिए धन्यवाद। –