Solved

Another thread question

Posted on 2011-03-10
3
207 Views
Last Modified: 2012-05-11
My question is around locking objects. I'm not clear when it is necessary and when it isn't in my particular case.

I have a loop that loops through a lst of vendors (see code below).

I also have a eventhandler that tells me when the thread is finished. I update the vendors last update dates and set it to CanStart = true.

My question is do I need to use the lock in the for loop where I'm starting the threads as well as what I'm doing in the event handler?

I just don't want to run into a locked condition.


For Each v In lstVendors
                If blnStopAllThreads = False AndAlso v.CanStart = True AndAlso _
                   UpdateQueue.Count > 0 AndAlso intUpdateThreadCnt < MaxUpdateThreads Then
                    v.CanStart = False
                    'Create a new thread
                    intUpdateThreadCnt += 1
                    Dim UThread As New UpdateThread("UpdateThread - " & CStr(intUpdateThreadCnt))
                    UThread.MyVendor = v
                    AddHandler UThread.ThreadCompleted, AddressOf UpdateThreadCompletedEventHandler
                    AddHandler UThread.ThreadVendorUpdated, AddressOf UpdateThreadVendorLastUpdateChangedEventHandler
                    Dim MyThread As Thread = New Thread(AddressOf UThread.UpdateData)
                    MyThread.Priority = ThreadPriority.Normal
                    MyThread.Name = "UpdateThread - " & CStr(intUpdateThreadCnt)
                    lstUpdateThreads.Add(MyThread)
                    MyThread.Start()
                End If

            Next








 Private Sub UpdateThreadVendorLastUpdateChangedEventHandler(ByVal v As Vendor)
        Dim i As Integer
        'LogOps.LogMessage("INFO", "ThreadVendorLastUpdateChangedEventHandler-ThreadCompletedEvent", "In Update Changed Event Handler")
        Try
            boolMonitorEntered = Monitor.TryEnter(MySyncLock, 15000)
            boolLockCreated = True

            If boolMonitorEntered Then
                For i = 0 To lstVendors.Count - 1
                    If lstVendors.Item(i).ForeignSysExchangeId = v.ForeignSysExchangeId Then
                        lstVendors.Item(i).LastAllergyUpdateDate = v.LastAllergyUpdateDate
                        lstVendors.Item(i).LastMedUpdateDate = v.LastMedUpdateDate
                        lstVendors.Item(i).LastRxUpdateDate = v.LastRxUpdateDate
                        lstVendors.Item(i).CanStart = True
                        Exit For
                    End If
                Next
            End If
        Finally
            If boolLockCreated = True Then
                Monitor.Exit(MySyncLock)
            End If
        End Try
    End Sub

Open in new window

0
Comment
Question by:rutledgj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35102274
Whether the lock is necessary or not depends on if more than one thread can ever satisfy this condition in your update handler at the same time:

    If lstVendors.Item(i).ForeignSysExchangeId = v.ForeignSysExchangeId Then

If more than one thread can end up matching the same "record" then you do need the lock if you wish to prevent them both writing to the same record and possibly only doing a partial update as they step on each other.

If every single thread will always be updating a unique record then the lock is not necessary.

One thread for every record is probably not a good approach though...  =\
How many are we talking about here?

A ThreadPool might be a better design.
0
 

Author Comment

by:rutledgj
ID: 35102615
Each thread operates on a single unique vendor which can contain any number of customers. My question isn't about having a lock in the eventhandler but in the for loop where I'm assigning the vendors to the threads.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35102717
Gotcha...no, you should not need a lock up there.

A different issue, though, is that you are using a For Each iterator in the starter and possibly modifying that collection in the update handler.  If the update handler is fired before all the threads are started it could cause an issue.

Using an indexed for loop in both places would probably fix that if the issue arises.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 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