473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
May 8 '06 #1
3 7091
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 ;).


"SatCom" <Sa****@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
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

May 8 '06 #2
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.
May 8 '06 #3
Thanks to you guys. I appreciate the help.
May 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Charles Law | last post by:
Hi guys. I'm back on the threading gig again. It's the age-old question about waiting for something to happen without wasting time doing it. Take two threads: the main thread and a worker...
0
by: Pedro CLIKEAR | last post by:
The problem happens when we try to set the password for two different users with DirectoryEntry.Invoke method. The first time we use user.Invoke("SetPassword",new object {pwdUsuario}); it works...
10
by: Abel | last post by:
I have this code string theClassName = "Rules.FillListBox.FillListBox" + theType + ", Rules" Type theClass = Type.GetType(theClassName) Object o = Activator.CreateInstance(theClass) object...
4
by: Erik | last post by:
First, I am not creating any new threads via invoke or any other intentional means. As far as I know, there is just one thread, which is the main thread. My windows form class creates a new...
0
by: Dave | last post by:
I noticed if I access a web service such as: https://MySite/WebServices/Service.asmx In the View Source, the Invoke button wants to submit the form to post to just "http". How do you preserve...
5
by: Mo | last post by:
Hi, I have an application where I read a serial port data from a barcode and set the labels on a form. I also have a textbox and button where you can enter the data and here is the problem. if I...
5
by: Mo | last post by:
I am trying to set a text box value when data is received from the com port (barcode reader). I am getting the following error when I try to set the text box TXNumber after data is received ...
7
by: Sin Jeong-hun | last post by:
I've using thread a lot in C#, but all of them were just single method with no return value or paramenters. It may sound bizarre but, can't it be a class? I'm writing an Windows application that...
3
by: =?Utf-8?B?Sm9l?= | last post by:
I know that I have posted this question before, but it is still unresolved and I don't know where to turn to next. I have code that is creating a user (works fine), then sets the account flags...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.