Solved

serial port receive data transfer to UI thread

Posted on 2013-01-28
4
1,464 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
ID: 38826658
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
ID: 38826802
winforms.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 38827124
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
ID: 38827421
thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

930 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

12 Experts available now in Live!

Get 1:1 Help Now