Hi All,
I'm building an app that generates 5 threads which all go off and fetch data from a list of web pages. I have three listviews: pages to process, pages processing and pages processed. At different points the threads need to shift one of the list view items from one list view to another. However this is where the problem occurs. From each of the threads created to fetch the pages, they raise an event on starting and another on finishing. These events are handled by the Form class. From there I invoke a delegate to update the appropriate listviews (code below).
Every so often an exception is thrown (IndexOutOfRange) on one or the listviews being updated. I've spent a long time trying to debug this, but have not been able to solve the problem. The list views are definitely being updated on the correct thread (i.e. the one that created them). It appears though, that when two threads raise an event at almost the same time they are overlapping on the GUI thread. eg. I've seen something similar to
being run, but then the line of code:
being run again, before the first is finished. Hence of data in he listview ha changed and the exception is thrown. It just seems TransferToProcessing routine doesn't properly finish. Can I lock the object somehow? I've tried:
But it makes no difference!! Anyone seen anything like this before, or know what I've doing wrong!? It's very frustrating!! Any help would be very much appreciated!
I'm building an app that generates 5 threads which all go off and fetch data from a list of web pages. I have three listviews: pages to process, pages processing and pages processed. At different points the threads need to shift one of the list view items from one list view to another. However this is where the problem occurs. From each of the threads created to fetch the pages, they raise an event on starting and another on finishing. These events are handled by the Form class. From there I invoke a delegate to update the appropriate listviews (code below).
Code:
Private Sub TransferToProcessingHandler(ByVal itm As ListViewItem) Handles HttpDispatcher.TransferToProcessing
If InvokeRequired Then
Invoke(New TransferToProcessingCallback(AddressOf TransferToProcessing), New Object() {itm})
Else
itm = lstProcessJobs.Items(itm.Index)
lstProcessJobs.Items(itm.Index).Remove()
lstRunning.Items.Add(itm)
End If
End Sub
Private Delegate Sub TransferToProcessingCallback(ByVal itm As ListViewItem)
Private Sub TransferToProcessing(ByVal itm As ListViewItem)
itm = lstProcessJobs.Items(itm.Index)
lstProcessJobs.Items(itm.Index).Remove()
lstRunning.Items.Add(itm)
End Sub
Every so often an exception is thrown (IndexOutOfRange) on one or the listviews being updated. I've spent a long time trying to debug this, but have not been able to solve the problem. The list views are definitely being updated on the correct thread (i.e. the one that created them). It appears though, that when two threads raise an event at almost the same time they are overlapping on the GUI thread. eg. I've seen something similar to
Code:
itm = lstProcessJobs.Items(itm.Index)
lstProcessJobs.Items(itm.Index).Remove()
Code:
itm = lstProcessJobs.Items(itm.Index)
Code:
Synclock lstRunning
itm = lstProcessJobs.Items(itm.Index)
lstProcessJobs.Items(itm.Index).Remove()
lstRunning.Items.Add(itm)
End Synclock