Solved

serial port receive data transfer to UI thread

Posted on 2013-01-28
4
1,422 Views
Last Modified: 2013-01-28
hi - i'm using vs2010 and .net 4. i'm implementing a serial interface device. i've set up a SerialPort() and am able to send and receive data. i'm using the sp.dataReceived event to read the return buffer. however, the receive method appears to exist in a seperate thread. if i try to update ui devices directly, i get a cross thread error. what's the recommended way to update the ui thread from the dataReceive event?
thanks,
Jim
0
Comment
Question by:jdcoburn
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
is it winforms or wpf?
this is a typical "UI cross-thread" access issue. In both cases, there is a well known solution to invoke the UI Thread from the secondary/background thread.
0
 

Author Comment

by:jdcoburn
Comment Utility
winforms.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
Here's a quick example of passing a string value to the main UI thread:
        private delegate void processValueDelegate(string value);

        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string value = sp.ReadLine();

            processValue(value);
        }

        private void processValue(string value)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new processValueDelegate(processValue), new object[] { value });
            }
            else
            {
                // ... safe to update GUI in here ...
                label1.Text = value;
            }
        }

Open in new window

0
 

Author Closing Comment

by:jdcoburn
Comment Utility
thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now