Solved

serial port receive data transfer to UI thread

Posted on 2013-01-28
4
1,674 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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