Solved

serial port receive data transfer to UI thread

Posted on 2013-01-28
4
1,765 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

632 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