Solved

vb.net BackgroundWorker and updating Listview

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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