views:

289

answers:

0

Hi all

I am trying to update a listview from my backgroundworker. On the ProgressChanged event, I am passing back ProgressPercentage and a custom object called OperationInfo in the UserState object.

OperationInfo has 4 properties: Operation(string), Success(boolean), Count(int), and AdditionalMessage(string)

The below code updates my progress bar, checks if the content of Operation is equal to the last Operation message that was passed to the listview. If so, update the other 3 properties in the listview. If not, add a new item to the listview using the values of OperationInfo.

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    tsPB.Value = e.ProgressPercentage

    Dim lvwitem As New ListViewItem

    If e.UserState IsNot Nothing Then
        Dim i As OperationInfo = DirectCast(e.UserState, OperationInfo)

        If lvwMessages.Items.Count > 0 AndAlso lvwMessages.Items(lvwMessages.Items.Count - 1).Text = i.Operation Then
            lvwMessages.BeginUpdate()
            lvwMessages.Items(lvwMessages.Items.Count - 1).SubItems(1).Text = i.Success
            lvwMessages.Items(lvwMessages.Items.Count - 1).SubItems(2).Text = e.ProgressPercentage
            lvwMessages.Items(lvwMessages.Items.Count - 1).SubItems(3).Text = i.AdditionalMsg
            lvwMessages.EndUpdate()
        Else
            lvwitem = New ListViewItem
            lvwitem.Text = i.Operation
            lvwitem.SubItems.Add(i.Success)
            lvwitem.SubItems.Add(i.Count)
            lvwitem.SubItems.Add(i.AdditionalMsg)

            lvwMessages.BeginUpdate()
            lvwMessages.Items.Add(lvwitem)
            lvwMessages.EndUpdate()

        End If

    End If
End Sub

The problem is that the listview doesn't update until the process is complete. What I want is for the Count column in the listview to be incrementing at the same time as the ProgressBar is incrementing, not updated with the final result at the end of the process. All of the data is coming through at the right times, it just seems like a threading problem whereby the Progress Bar on the UI thread is repainting, but the ListView which is presumably on the same thread is not.