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

F

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)
 
F

fniles

Thanks. It works.
I use asyncresult based on the example I got from the internet.
Is BeginInvoke faster than plain Invoke ?
 
C

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)
 
C

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.
 

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