Serial Port - How To Use "Invoke" On The Second Thread

G

Guest

Hello,
I had originally posted this in the winforms.controls discussion, forgive
the double post, Here is where I need help...

I have been porting some VB6 to VB2005 and here is the issue with the serial
port.
I am looking to update controls on the main form (textbox, listbox, etc)
when a call comes in. I can retrieve the ring status, caller-id etc, but
when I go to update a textbox, I receive a threading exception issue. As I
understand it, the "datareceived" event is raised on a second thread so I can
not directly update controls on the main form or (first thread) without using
INVOKE. My question is How do I do that?

Private Sub m_CommPort_DataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles
m_CommPort.DataReceived
Dim strModemRecBuffer As String = ""
strModemRecBuffer = m_CommPort.ReadExisting
....
....Extract the data from the string
....
....I realize this next line is the (Thread exception) since the control is
on the main form.
txtIncomingCall.text = strModemRecBuffer(index)

So how do I use invoke to update the controls on the first thread?
Any help would be appreciated Thanks,

SatCom
 
F

Fred Hedges

The best way is to create a method to call on the main thread and then
invoke a delegate to it on the other thread. For example, create a delegate
in your secondary thread and a method to execute the delegate. Ensure your
second thread has a reference to the main form instance (or some other
control come to think of it)......

Private Delegate Sub DoSomething_Delegate(ByVal theParameter1 As String,
theParameter2 as integer, theParameter3 as MyThing)


.... and then....


Public Sub Invoke_DoSomething(ByVal theParameter1 As String, theParameter2
as integer, theParameter3 as MyThing)

Dim Parameters(2) As Object

Parameters(0) = theParameter1
Parameters(1) = p
Parameters(2) = theParameter3

Try


m_AnInstanceOftheFormToInvokeOn.Invoke(New
DoSomething_Delegate(AddressOf m_Manager.Form.DoSomething), Parameters)


Catch ex As Exception

' It didneee work.

End Try


End Sub




.... finally, on your main form, you need a method, DoSomething:

Public Sub DoSomething (ByVal theParameter1 As String, theParameter2 as
integer, theParameter3 as MyThing)

End Sub



Now, at least this was the .NET 1.1 way of doing things. No doubt it's
become really easy with .NET 2.0 so forgive me if this approach has been
deprecated ;).
 
D

Dick Grier

Hi,

See my reply in the framework newsgroup. I have a VB .NET example on my
website that you can download that shows how to do this.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 

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

Similar Threads


Top