JimmyJimm JimmyJimm - 1 month ago 16
Vb.net Question

InvokeRequired not returning value from method

I use InvokeRequired for thread safety but when it comes to my method and if i pass value as true it always showing me false. Why and how to fix it?

Public Sub InitGrid(Optional myValue As Boolean = False)
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf InitGrid))
Else
MsgBox(myValue.ToString()) //always false...
End If
End Sub

Answer

You can use an Action(of T) version:

Public Sub InitGrid(Optional myValue As Boolean = False)
  If Me.InvokeRequired Then
    Me.Invoke(New Action(Of Boolean)(AddressOf InitGrid), myValue)
  Else
    MessageBox.Show(myValue.ToString())
  End If
End Sub