Solved

vb.net BackgroundWorker and updating Listview

Posted on 2014-03-31
2
758 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 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