Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

serial port receive data transfer to UI thread

Posted on 2013-01-28
4
Medium Priority
?
2,008 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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

824 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