• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 937
  • Last Modified:

vb.net BackgroundWorker and updating Listview

Hi all,
I trying to run a thread/Threads that call a procedure and get values from a ListViewItem and update that ListViewitem.  If I place a messagebox in calling the ListviewItem's subitem values I get data back; but It does not return data to the ListViewItem.

So basically I have my Backgroundworker thread called and within it a Procedure that passes in the ListviewItem.  No Errors; just not updating the ListviewItems properties.

I created the

Public Class ArgumentType
        Public _li As ListViewItem
    End Class

and pass in the listviewitem.  As I said I can show, in message box every property; but when I try to update one or more subitems values, or the image nothing happens.

Any ideas?
0
Thunder724
Asked:
Thunder724
1 Solution
 
Fernando SotoCommented:
Without seeing the code we would not have any idea, please show the code so that we can see what is going on.
0
 
ArkCommented:
Not sure if I understood correctly, but try using deep copy of objects:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListView1.View = View.Details
        ListView1.Columns.Add("Item", 120)
        ListView1.Columns.Add("SubItem_1", 150)
        ListView1.Columns.Add("SubItem_2", 150)
        For i = 1 To 10
            With ListView1.Items.Add("LV_Item#" & i.ToString)
                .SubItems.Add(String.Format("Subitem_{0}_1_Initial", i))
                .SubItems.Add(String.Format("Subitem_{0}_2_Initial", i))
            End With
        Next
        BackgroundWorker1.WorkerReportsProgress = True
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim items = CType(e.Argument, ListViewItem())
        For i = 1 To 100
            For Each li As ListViewItem In items
                li.Text = "BG_Item #" & i.ToString
                li.SubItems(1).Text = "BG_SubItem_1 #" & i.ToString
                li.SubItems(2).Text = "BG_SubItem_2 #" & i.ToString
            Next
            BackgroundWorker1.ReportProgress(i, items)
            Threading.Thread.Sleep(500)
        Next
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Dim items As ListViewItem() = CType(e.UserState, ListViewItem())
        For i = 0 To items.Count - 1
            ListView1.Items(i) = DeepClone(Of ListViewItem)(items(i))
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lstItems As New List(Of ListViewItem)
        For Each item As ListViewItem In ListView1.Items
            lstItems.Add(item)
        Next
        BackgroundWorker1.RunWorkerAsync(DeepClone(Of ListViewItem())(lstItems.ToArray))
     End Sub

    Private Function DeepClone(Of ObjectType)(ByVal source As Object) As ObjectType
        Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Using ms As New IO.MemoryStream
            bf.Serialize(ms, CType(source, ObjectType))
            ms.Position = 0
            Return CType(bf.Deserialize(ms), ObjectType)
        End Using
    End Function

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now