Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here it is in VB.net, very similar to Samuel's answer. I have four overloads depending on whether you want a subroutine or function and whether or not there's a parameter. It would be easy to add more overloads for more parameters. VB.Net is able to infer the types.</p> <pre><code>Module ISynchronizeInvokeExtensions Public Delegate Function GenericLambdaFunctionWithParam(Of InputType, OutputType)(ByVal input As InputType) As OutputType Private Delegate Function InvokeLambdaFunctionCallback(Of InputType, OutputType)(ByVal f As GenericLambdaFunctionWithParam(Of InputType, OutputType), ByVal input As InputType, ByVal c As System.ComponentModel.ISynchronizeInvoke) As OutputType Public Function InvokeEx(Of InputType, OutputType)(ByVal f As GenericLambdaFunctionWithParam(Of InputType, OutputType), ByVal input As InputType, ByVal c As System.ComponentModel.ISynchronizeInvoke) As OutputType If c.InvokeRequired Then Dim d As New InvokeLambdaFunctionCallback(Of InputType, OutputType)(AddressOf InvokeEx) Return DirectCast(c.Invoke(d, New Object() {f, input, c}), OutputType) Else Return f(input) End If End Function Public Delegate Sub GenericLambdaSubWithParam(Of InputType)(ByVal input As InputType) Public Sub InvokeEx(Of InputType)(ByVal s As GenericLambdaSubWithParam(Of InputType), ByVal input As InputType, ByVal c As System.ComponentModel.ISynchronizeInvoke) InvokeEx(Of InputType, Object)(Function(i As InputType) As Object s(i) Return Nothing End Function, input, c) End Sub Public Delegate Sub GenericLambdaSub() Public Sub InvokeEx(ByVal s As GenericLambdaSub, ByVal c As System.ComponentModel.ISynchronizeInvoke) InvokeEx(Of Object, Object)(Function(i As Object) As Object s() Return Nothing End Function, Nothing, c) End Sub Public Delegate Function GenericLambdaFunction(Of OutputType)() As OutputType Public Function InvokeEx(Of OutputType)(ByVal f As GenericLambdaFunction(Of OutputType), ByVal c As System.ComponentModel.ISynchronizeInvoke) As OutputType Return InvokeEx(Of Object, OutputType)(Function(i As Object) f(), Nothing, c) End Function End Module </code></pre> <p>Usage (run this in a backgroundworker):</p> <pre><code> InvokeEx(Sub(x As String) Me.Text = x, "foo", Me) 'set form title to foo InvokeEx(AddressOf MsgBox, Me.Text, Me) InvokeEx(Sub() Me.Text &amp;= "!", "foo", Me) 'append "!" to form title InvokeEx(AddressOf MsgBox, Me.Text, Me) Dim s As String = InvokeEx(Function() Me.Text, Me) &amp; "bar" 'get form title to backgorundworker thread InvokeEx(AddressOf MsgBox, s, Me) 'display the string from backgroundworker thread </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload