Solved

vb.net BackgroundWorker and updating Listview

Posted on 2014-03-31
2
748 Views
Last Modified: 2014-04-17
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
Comment
Question by:Thunder724
2 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39968342
Without seeing the code we would not have any idea, please show the code so that we can see what is going on.
0
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
ID: 39968707
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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