rutledgj
asked on
vb.net threading question
I'm trying to ensure that I'm using synclock correctly.
I have the following class
public class MyThreadTest
private MyLockObject as new Object
private lstOrgs as new List(of Org)
Private AllergyUpdateThreadPool(Ma xAllergyUp dateThread s) As Thread
Private VendorCount as integer
Public sub New()
Dim x As Integer
For x = 0 To UBound(AllergyUpdateThread Pool) - 1
AllergyUpdateThreadPool(x) = New Thread(AddressOf UpdateAllergy)
AllergyUpdateThreadPool(x) .Name = "DrFirstAllergyUpdateThrea d #" + CStr(x)
' AllergyUpdateThreadPool(x) .Start()
Next
end sub
public sub StartThreads(VendorList as list (of Org))
dim x as integer
SyncLock (MySyncLock)
lstOrgs .Clear()
LoadVendorList(VendorList)
VendorCount = -1
End SyncLock
For x = 0 To UBound(AllergyUpdateThread Pool) - 1
AllergyUpdateThreadPool(x) .Start()
Next
end sub
Private Sub UpdateAllergy()
SyncLock (MySyncLock)
' v = OperationQueue.Dequeue()
If VendorCount = lVendors.Count - 1 Then
VendorCount = 0
Else
VendorCount += 1
End If
v = lVendors(VendorCount)
End SyncLock
'Do some work
end sub
In Main form
Dim t as new MyThreadTest
Dim MyVendorList as new List (of Org)
'Load vendor list
t.StartAllergyUpdateProces s(MyVendor List)
Questions
1. Does this look like a safe approach for using the MyLockObject object for multiple threads in this class? Is it declared correctly or does it need to be a shared or protected variable?
2. Any other areas of concern (Note. this is a simplified version of what I'm doing)
I have the following class
public class MyThreadTest
private MyLockObject as new Object
private lstOrgs as new List(of Org)
Private AllergyUpdateThreadPool(Ma
Private VendorCount as integer
Public sub New()
Dim x As Integer
For x = 0 To UBound(AllergyUpdateThread
AllergyUpdateThreadPool(x)
AllergyUpdateThreadPool(x)
' AllergyUpdateThreadPool(x)
Next
end sub
public sub StartThreads(VendorList as list (of Org))
dim x as integer
SyncLock (MySyncLock)
lstOrgs .Clear()
LoadVendorList(VendorList)
VendorCount = -1
End SyncLock
For x = 0 To UBound(AllergyUpdateThread
AllergyUpdateThreadPool(x)
Next
end sub
Private Sub UpdateAllergy()
SyncLock (MySyncLock)
' v = OperationQueue.Dequeue()
If VendorCount = lVendors.Count - 1 Then
VendorCount = 0
Else
VendorCount += 1
End If
v = lVendors(VendorCount)
End SyncLock
'Do some work
end sub
In Main form
Dim t as new MyThreadTest
Dim MyVendorList as new List (of Org)
'Load vendor list
t.StartAllergyUpdateProces
Questions
1. Does this look like a safe approach for using the MyLockObject object for multiple threads in this class? Is it declared correctly or does it need to be a shared or protected variable?
2. Any other areas of concern (Note. this is a simplified version of what I'm doing)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm not a DB guy but it seems to me that you should have just one instance for the class that all the threads share. You may need to place calls to the DB within SyncLock blocks to make sure they don't write over each other.
ASKER
Ok. thanks. I think I'll post another question specifically for that.
ASKER
One other question if you don't mind. I aslo will be using a database class to perform database activities within the thread.
Should the database class be instantiated within the UpdateAllergy function (One dbOps object for each thread) or can I declare it once within the whole class and each thread use the same connection?