Solved

vb.net threading question

Posted on 2011-02-23
4
229 Views
Last Modified: 2012-06-27
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(MaxAllergyUpdateThreads) As Thread
   Private VendorCount as integer


   Public sub New()
          Dim x As Integer
                For x = 0 To UBound(AllergyUpdateThreadPool) - 1
                    AllergyUpdateThreadPool(x) = New Thread(AddressOf UpdateAllergy)
                    AllergyUpdateThreadPool(x).Name = "DrFirstAllergyUpdateThread #" + 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(AllergyUpdateThreadPool) - 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.StartAllergyUpdateProcess(MyVendorList)



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)
0
Comment
Question by:rutledgj
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34962300
The SyncLock portion will work just fine.  You would need it to be Shared if your List was shared and being used across multiple instances of MyThreadTest.  Since everything is contained within this class it will work as expected.

The counter logic, though, looks off to me.  If you pass in an empty list then both VendorCount and (lVendors.Count - 1) would be -1.  In that case you set VendorCount = 0 and then attempt to access the List at that Index which of course would cause an exception since the List is empty.
0
 

Author Comment

by:rutledgj
ID: 34962454
Thanks, You are right about the empty list. I will add a check for that.

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?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34962506
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.
0
 

Author Closing Comment

by:rutledgj
ID: 34962767
Ok. thanks. I think I'll post another question specifically for that.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now