Solved

vb.net BackgroundWorker and updating Listview

Posted on 2014-03-31
2
816 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 63

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 28

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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