Invoke with arguments crashes application.

P

Phillip Taylor

I have an application which displays a ListView control on a form. I
start a second thread to run in the background and populate the
control. When the second thread finishes it's work, it invokes a
method which should populate the control. When I perform the
callback / procedure call it passes a LinkedList as a parameter, which
doesn't convert into an Object array properly. Even though i can't see
why it wouldn't. The line "me.invoke(method, New Object() { records })
crashes the problem with:

"The value of argument 'value' (-1) is invalid for Enum type 'View'.
Parameter name: value"

My code looks like this:

--DELEGATE

Public Delegate Sub MigrationCompleteEventHandler(ByVal records As
LinkedList(Of ListViewItem))

--IN SECOND, CALLING THREAD

processComplete.Invoke(returnedListViewItems)

--UNDER FORM, IN ORIGINAL THREAD

Public Sub ConversionUpdateComplete(ByRef records As LinkedList(Of
ListViewItem))
If (Me.InvokeRequired) Then
Dim method As New MigrationCompleteEventHandler(AddressOf
ConversionUpdateComplete)
If (records.Count > 0) Then
Me.Invoke(method, New Object() {records}) <-- THIS
DOEN'T WORK.
End If
Else

lvUpdateSuggestions.Items.Clear()

For Each l As ListViewItem In records
lvUpdateSuggestions.Items.Add(l)
Next

lvUpdateSuggestions.View = True

pbConverting.Visible = False
lblConversionWait.Visible = False
lblSuggestionNotice.Visible = True
btnNext.Enabled = True
End If
End Sub
 
A

Armin Zingler

Phillip Taylor said:
I have an application which displays a ListView control on a form. I
start a second thread to run in the background and populate the
control. When the second thread finishes it's work, it invokes a
method which should populate the control. When I perform the
callback / procedure call it passes a LinkedList as a parameter,
which doesn't convert into an Object array properly. Even though i
can't see why it wouldn't. The line "me.invoke(method, New Object()
{ records }) crashes the problem with:

"The value of argument 'value' (-1) is invalid for Enum type 'View'.
Parameter name: value"

My code looks like this:

--DELEGATE

Public Delegate Sub MigrationCompleteEventHandler(ByVal records As
LinkedList(Of ListViewItem))

--IN SECOND, CALLING THREAD

processComplete.Invoke(returnedListViewItems)

--UNDER FORM, IN ORIGINAL THREAD

Public Sub ConversionUpdateComplete(ByRef records As LinkedList(Of
ListViewItem))
If (Me.InvokeRequired) Then
Dim method As New MigrationCompleteEventHandler(AddressOf
ConversionUpdateComplete)
If (records.Count > 0) Then
Me.Invoke(method, New Object() {records}) <-- THIS
DOEN'T WORK.
End If
Else

lvUpdateSuggestions.Items.Clear()

For Each l As ListViewItem In records
lvUpdateSuggestions.Items.Add(l)
Next

lvUpdateSuggestions.View = True

pbConverting.Visible = False
lblConversionWait.Visible = False
lblSuggestionNotice.Visible = True
btnNext.Enabled = True
End If
End Sub


After changing the ByRef arg to ByVal - otherwise it does not even compile -
and after adding

Dim processComplete As New MigrationCompleteEventHandler(AddressOf
ConversionUpdateComplete)
Dim returnedListViewItems As New LinkedList(Of ListViewItem)

and adding an item to returnedListViewItems, I was able to execute the code.
It worked without a problem.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top