Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Add Item To ListView Cross Threads

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 :)
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?
Lotta blood to let, peace to make, give a fuk about a check
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
(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
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)
Then you are doing something completely wrong. You are also referring to another form.
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();
(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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by FarosMolokoNov 2, 2018 02:28 PM 4 174 Nov 2, 2018 02:28 PM
Last Post: He B TeMy
[.NET] by Johan CruyffJun 14, 2017 09:41 AM 5 205 Jun 14, 2017 09:41 AM
Last Post: Johan Cruyff
[C#] by IAmBabyFinnApr 10, 2016 09:50 AM 5 202 Apr 10, 2016 09:50 AM
Last Post: IAmBabyFinn