Hack Forums
[.NET] Add Item To ListView Cross Threads - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Add Item To ListView Cross Threads (/showthread.php?tid=5181841)



Add Item To ListView Cross Threads - 270z - Feb 23, 2016

So I am scraping sites then collecting data and sending them to a listview. but when I do this with a thread it does not send them to the listview...

No thread: works
with thread: No works

Here is my delegate:

Code
Private Delegate Sub ListViewAddItem_delegate(ByVal Title As String, ByVal Content As String)
    Private Sub ListViewAddItem(ByVal Title As String, ByVal Content As String)
        If CampaignForm.ListView1.InvokeRequired Then
            Dim d As New ListViewAddItem_delegate(AddressOf ListViewAddItem)
            CampaignForm.ListView1.BeginInvoke(d, {Title, Content})
        Else
            Dim lvi As New ListViewItem(Title)
            lvi.SubItems.Add(Content)
            CampaignForm.ListView1.Items.AddRange(New ListViewItem() {lvi})
            CampaignForm.ListView1.Update()
        End If
    End Sub

Inside the background worker I use this code:
Code
ListViewAddItem(Title, CountWords(Body))

If someone sees something I am doing wrong please say :)


* - BahNahNah - Feb 23, 2016

Either just ude Invoke or you need a EndInvoke method.
Also, why is this a delegate and why are you using AddRange for one item?


* - Cairns - Feb 24, 2016

Private Delegate Sub ListViewAddItem_delegate(ByVal Title As String, ByVal Content As String)
Private Sub ListViewAddItem(ByVal Title As String, ByVal Content As String)
If InvokeRequired Then
invoke(new ListViewAddItem_delegate(addressof listviewadditem),title,content)
return
end if

Dim lvi As New ListViewItem(Title)
lvi.SubItems.Add(Content)
CampaignForm.ListView1.Items.Add(lvi)
CampaignForm.ListView1.Update()
End Sub


* - 270z - Feb 24, 2016

(Feb 24, 2016 04:30 AM)Cairns Wrote:  Private Delegate Sub ListViewAddItem_delegate(ByVal Title As String, ByVal Content As String)
Private Sub ListViewAddItem(ByVal Title As String, ByVal Content As String)
If InvokeRequired Then
invoke(new ListViewAddItem_delegate(addressof listviewadditem),title,content)
return
end if

Dim lvi As New ListViewItem(Title)
lvi.SubItems.Add(Content)
CampaignForm.ListView1.Items.Add(lvi)
CampaignForm.ListView1.Update()
End Sub

Still does not add anything to the listview...

I can't think of a reason why that wouldn't work hmm


* - Ninja - Feb 24, 2016

You can just do a normal invoke:
Code
Invoke(Sub()
Dim item As New ListViewItem(Title)
item.SubItems.Add(Content)
CampaignForm.ListView1.Items.Add(item)
End Sub)



* - Cairns - Feb 24, 2016

Then you are doing something completely wrong. You are also referring to another form.


* - drake08 - Feb 24, 2016

I kind of hate vb so I always provide answers in C# since I know this language pretty good.I hope that's not an issue for you.

Code
delegate void ListViewAddItem(String title, String content);

private void AddLViewItem(String title, String content) {
  if (lstView.InvokeRequired)
    Invoke(new ListViewAddItem(AddLViewItem), title, content);
  else
    lstView.Items.Add(new ListViewItem(new String[] { title, content }));
}
And this is how you can test it:

new Thread(() => { AddLViewItem("item", "subitem"); }).Start();


* - Ninja - Feb 24, 2016

(Feb 24, 2016 01:40 PM)270z Wrote:  Yes I am

You ignored my post. And the code I posted works for me when cross-thread accessing a listview.