0

I'm using a BackgroundWorker and I need to use a Delegate Function to see if a ListViewItem is checked or not but I keep recieving a cross-thread error. It must be the way I'm writing it. Any help?

    Dim delListViewItemChecked As ListViewItemCheckedDelegate = AddressOf ListViewItemChecked
    delListViewItemChecked.Invoke(ListViewPhotos, 0)

Private Delegate Function ListViewItemCheckedDelegate(ByVal listView As ListView, ByVal index As Integer) As Boolean

Private Function ListViewItemChecked(ByVal listView As ListView, ByVal index As Integer) As Boolean
    If listView.Items(index).Checked = True Then
        Return True
    Else
        Return False
    End If
End Function

1 Answers1

0

Try this:

  1. Do not pass the listView as a parameter to ListViewItemCheckedDelegate.
  2. Declare a new delegate instance inside the DoWork handler of your background worker.

This sample seems to work OK:

Private Delegate Function ListViewItemCheckedDelegate(ByVal index As Integer) As Boolean

Private Function ListViewItemChecked(ByVal index As Integer) As Boolean
Return ListView1.Visible
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
bkg1.RunWorkerAsync()
End Sub

Private Sub bkg1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bkg1.DoWork
Dim delListViewItemChecked As New ListViewItemCheckedDelegate(AddressOf ListViewItemChecked)
MsgBox(Me.Invoke(delListViewItemChecked, 3)) ' arbitrary 3
End Sub
xpda
  • 15,585
  • 8
  • 51
  • 82