L
Luc
I just found that passing arguments by reference to a function across
object.Invoke doesn't work (or rather, getting them back *from* the
function is what doesn't work).
Is that by design?
What do you think is the best way to get more data than just a simple
return value back from a function that's called by invoke?
I thought of passing in a class as argument (ByVal) and returning an
updated copy as return value.
I think a code sample illustrates it best.
The code below prints these two lines in the immediate window:
Same-thread call: result 1, string 'Succeeded'
Cross-thread invoked call: result 1, string 'Failed'
The code calls the same function twice, first directly, then it starts a
worker thread from where it's called through Me.Invoke.
Start a new VS2008 windows forms project, paste the code below in the
form's code window.
Line continuation is used only to prevent long lines from being broken by
my newsreader, you may want to re-join those lines for better readability.
Imports System.Threading
Public Class Form1
Private MyThread As Thread
Private Shared StaticString As String
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
StaticString = "Failed"
Dim rv As Integer = Func1(StaticString)
Debug.WriteLine("Same-thread call: result " & rv _
& ", string '" & StaticString & "'")
MyThread = New Thread(AddressOf ThreadMain)
MyThread.Start()
End Sub
Private Sub ThreadMain()
Dim rv As Integer = Func1(StaticString)
StaticString = "Failed"
Debug.WriteLine("Cross-thread invoked call: result " & rv _
& ", string '" & StaticString & "'")
End Sub
Private Delegate Function dFunc1(ByRef s As String) As Integer
Private Function Func1(ByRef s As String) As Integer
If Me.InvokeRequired Then
Return DirectCast(Me.Invoke(New dFunc1(AddressOf Func1), _
s), Integer)
Else
s = "Succeeded"
Return 1
End If
End Function
End Class
object.Invoke doesn't work (or rather, getting them back *from* the
function is what doesn't work).
Is that by design?
What do you think is the best way to get more data than just a simple
return value back from a function that's called by invoke?
I thought of passing in a class as argument (ByVal) and returning an
updated copy as return value.
I think a code sample illustrates it best.
The code below prints these two lines in the immediate window:
Same-thread call: result 1, string 'Succeeded'
Cross-thread invoked call: result 1, string 'Failed'
The code calls the same function twice, first directly, then it starts a
worker thread from where it's called through Me.Invoke.
Start a new VS2008 windows forms project, paste the code below in the
form's code window.
Line continuation is used only to prevent long lines from being broken by
my newsreader, you may want to re-join those lines for better readability.
Imports System.Threading
Public Class Form1
Private MyThread As Thread
Private Shared StaticString As String
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
StaticString = "Failed"
Dim rv As Integer = Func1(StaticString)
Debug.WriteLine("Same-thread call: result " & rv _
& ", string '" & StaticString & "'")
MyThread = New Thread(AddressOf ThreadMain)
MyThread.Start()
End Sub
Private Sub ThreadMain()
Dim rv As Integer = Func1(StaticString)
StaticString = "Failed"
Debug.WriteLine("Cross-thread invoked call: result " & rv _
& ", string '" & StaticString & "'")
End Sub
Private Delegate Function dFunc1(ByRef s As String) As Integer
Private Function Func1(ByRef s As String) As Integer
If Me.InvokeRequired Then
Return DirectCast(Me.Invoke(New dFunc1(AddressOf Func1), _
s), Integer)
Else
s = "Succeeded"
Return 1
End If
End Function
End Class