M
Mathijs Beentjes
Hi all,
In a mulithreaded application I tried to report back messages to the
main Thread, by using Control.Invoke.
I built a simple form and created the code displayed below.
In this code, after pressing a button (button2), I do the following:
- I create a new Thread (6)
- From this new thread, I save the thread number of this new
thread in the string with name buffer (5) and then want to call the
Control.Invoke method (5 as well).
- As far as I understand, I expected an update of the two labels (label1
and label2), containing different thread numbers (main thread number in
label1, calling thread number in label2).
The following things go wrong:
- Using this code, I get an InvalidArgument Exception on the moment I
call Me.Invoke
- When I typed in the code Me.Invoke(, you normally see a list of
overloaded functions. According to the documentation, there should be
two overloaded methods. However, I see only one: The invoke which is
used if you don't want to pass any parameters at all.
- I thought: may be I don't understand and should I just call the
delegate directly, like the commented out line in 6). I replaced the
Me.Invoke by a direct call to the delegate. This gave the following
result: no error message, no freezing of application, but the
show_message function now seems to run in the calling thread (instead of
the main thread). But apart from this it seems to work fine, also in my
real application.
I have the following question:
- why does me.invoke not work?????
Thanks in advance,
Mathijs Beentjes ([email protected])
1)
Private Delegate Sub myDelegate()
2)
Private buffer as String
3)
' Gets the current thread
private function getThread()
return Thread.CurrentThread.GetHashCode.ToString
end function
4)
' Updates the main form
Private Sub show_Message()
Label1.Text = "Main: " & getThread
Label2.Text = "Calling Thread: " & buffer
End Sub
5)
Private Sub HandleThread1()
Dim aDelegate As New myDelegate(AddressOf show_Message)
buffer = getThread
'aDelegate()
Me.Invoke(aDelegate)
End Sub
6)
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim t As Thread = New Thread(AddressOf Me.HandleThread1)
t.Start()
End Sub
In a mulithreaded application I tried to report back messages to the
main Thread, by using Control.Invoke.
I built a simple form and created the code displayed below.
In this code, after pressing a button (button2), I do the following:
- I create a new Thread (6)
- From this new thread, I save the thread number of this new
thread in the string with name buffer (5) and then want to call the
Control.Invoke method (5 as well).
- As far as I understand, I expected an update of the two labels (label1
and label2), containing different thread numbers (main thread number in
label1, calling thread number in label2).
The following things go wrong:
- Using this code, I get an InvalidArgument Exception on the moment I
call Me.Invoke
- When I typed in the code Me.Invoke(, you normally see a list of
overloaded functions. According to the documentation, there should be
two overloaded methods. However, I see only one: The invoke which is
used if you don't want to pass any parameters at all.
- I thought: may be I don't understand and should I just call the
delegate directly, like the commented out line in 6). I replaced the
Me.Invoke by a direct call to the delegate. This gave the following
result: no error message, no freezing of application, but the
show_message function now seems to run in the calling thread (instead of
the main thread). But apart from this it seems to work fine, also in my
real application.
I have the following question:
- why does me.invoke not work?????
Thanks in advance,
Mathijs Beentjes ([email protected])
1)
Private Delegate Sub myDelegate()
2)
Private buffer as String
3)
' Gets the current thread
private function getThread()
return Thread.CurrentThread.GetHashCode.ToString
end function
4)
' Updates the main form
Private Sub show_Message()
Label1.Text = "Main: " & getThread
Label2.Text = "Calling Thread: " & buffer
End Sub
5)
Private Sub HandleThread1()
Dim aDelegate As New myDelegate(AddressOf show_Message)
buffer = getThread
'aDelegate()
Me.Invoke(aDelegate)
End Sub
6)
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim t As Thread = New Thread(AddressOf Me.HandleThread1)
t.Start()
End Sub