Using Multithreading with Controls (Control.Invoke, VB.NET)

  • Thread starter Mathijs Beentjes
  • Start date
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
 
D

Dick Grier

Hi,

The Compact Framework is more limited than the Desktop. You cannot pass
arguments (until later this year, with CF 2). What you have to do is to
Invoke an event handler, such as:

Me.Invoke(New EventHandler(AddressOf DisplayData))

'


Private Sub DisplayData(ByVal sender As Object, ByVal e As EventArgs)


'stuff
End Sub


The consequence of this is that you have to pass data into the delegate
(DisplayData in this example) by using variables visible to its code....
And, 'stuff has to sort out these in lieu of arguments.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
www.mabry.com/vbpgser4 to order.
 
M

Mathijs Beentjes

Hi,

Thanks to Richard Grier, I rewrote my code and it works now. Thanks!

The code below is compiled & tested and it works.

Regards,

Mathijs Beentjes.


Private buffer As String

' Gets the current thread
Private Function getThread()
Return Thread.CurrentThread.GetHashCode.ToString
End Function

' Updates the main form
Private Sub show_Message(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = "Main: " & getThread()
Label2.Text = "Calling Thread: " & buffer
End Sub

Private Sub HandleThread1()
buffer = getThread()
Me.Invoke(New EventHandler(AddressOf show_Message))
End Sub

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
 

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