Passing Parameters using Invoke in multi-threaded app

  • Thread starter Thread starter Phillip Taylor
  • Start date Start date
P

Phillip Taylor

I'm having a problem with this code. I can't really understand why it
won't work if, for example, I pass in a linked list of 0 records.

Public Sub ConversionUpdateComplete(ByRef records As LinkedList(Of
ListViewItem))
If (Me.InvokeRequired) Then
Dim method As New MigrationCompleteEventHandler(AddressOf
ConversionUpdateComplete)
Me.Invoke(method, New Object() {records})
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
 
I'm having a problem with this code. I can't really understand why it
won't work if, for example, I pass in a linked list of 0 records.


In anycase, what is happening in your app?

You should use BeginInvoke instead. Invoke may deadlock your app under
load.
 
Phillip Taylor said:
I'm having a problem with this code. I can't really understand why
it won't work if, for example, I pass in a linked list of 0 records.

What does "won't work" mean? The code looks ok (although I don't know why
the arg is declared ByRef).
Public Sub ConversionUpdateComplete(ByRef records As
LinkedList(Of ListViewItem))
If (Me.InvokeRequired) Then
Dim method As New MigrationCompleteEventHandler(AddressOf
ConversionUpdateComplete)
Me.Invoke(method, New Object() {records})
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


Armin
 
I have seern lots of strange behavior of .net apps when used byref
parameters , as i also do not see the point why it should be passed byref in
this case , i recomend you to declare the parameter byval and try again it
might / or might not solve your problem :-)


Regards

Michel Posseth
 
Back
Top