Home All Groups Group Topic Archive Search About

How to call BeginInvoke for a sub that has a parameter ?

Author
4 Jun 2006 8:25 PM
fniles
How can I call BeginInvoke for a sub that has a parameter ?
I have a sub WriteInput that change the Text field txtInput with the value
passed in its parameter.
How can I call WriteInput using BeginInvoke ?

The following codes gave me "parameter count mismatch" error.

Public Delegate Sub WriteLineDelegate2(ByVal data() As Object)

    Public Sub WriteInputGL(ByVal data() As Object)
        Dim ar1 As IAsyncResult

        ar1 = BeginInvoke(New WriteLineDelegate2(AddressOf WriteInput),
data)
    End Sub

    Private Sub WriteInput(ByVal data() As Object)
        Dim Line As String

        Line = CType(data(0), String)
        Me.txtInput.Text = Line
    End Sub

Dim arrObject() As Object

arrObject(0) = "this is a test"
WriteInputGL(arrObject)

Author
5 Jun 2006 8:49 PM
Charlie Brown
Not sure why you are using the asyncresult but here is what can be done

Public Delegate Sub WriteLineDelegate2(ByVal data() As Object)


    Public Sub WriteInputGL(ByVal data() As Object)
        Dim worker As New WriteLineDelegate2(AddressOf WriteInput)
        worker.BeginInvoke(data, Nothing, Nothing)
    End Sub


    Private Sub WriteInput(ByVal data() As Object)
        Dim Line As String
        Line = CType(data(0), String)
        Me.txtInput.Text = Line
    End Sub


    Dim arrObject() As Object


arrObject(0) = "this is a test"
WriteInputGL(arrObject)
Author
4 Jun 2006 10:12 PM
fniles
Thanks. It works.
I use asyncresult based on the example I got from the internet.
Is BeginInvoke faster than plain Invoke ?


Show quoteHide quote
"Charlie Brown" <cbr***@duclaw.com> wrote in message
news:1149540550.054711.32060@f6g2000cwb.googlegroups.com...
> Not sure why you are using the asyncresult but here is what can be done
>
> Public Delegate Sub WriteLineDelegate2(ByVal data() As Object)
>
>
>    Public Sub WriteInputGL(ByVal data() As Object)
>        Dim worker As New WriteLineDelegate2(AddressOf WriteInput)
>        worker.BeginInvoke(data, Nothing, Nothing)
>    End Sub
>
>
>    Private Sub WriteInput(ByVal data() As Object)
>        Dim Line As String
>        Line = CType(data(0), String)
>        Me.txtInput.Text = Line
>    End Sub
>
>
>    Dim arrObject() As Object
>
>
> arrObject(0) = "this is a test"
> WriteInputGL(arrObject)
>
Author
6 Jun 2006 12:36 AM
Charlie Brown
Invoke will run synchronously meaning it will invoke a new thread, but
you will have to wait for that thread to finish before continuing,
while BeginInvoke runs asynchronously and returns without waiting for
the Invoked thread to finish.