Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey everyone,

I'm trying to send the value from a serial port via myPort.DataReceived() to a textlabel on my windows form. Error is of course, that I can't access the control from a different thread. I know i need to use invoke for that but can't get it right. I tried this:
C#
void startlistening()
{    
   port.DataReceived.BeginInvoke += new SerialDataReceivedEventHandler (port_DataReceived);
}

void port_DataReceived()
{
    label.Text = port.ReadExisting();
}
Posted
Updated 13-Apr-12 9:26am
v2

1 solution

The easiest way is to set up a method for it:
C#
private void SetLabelText(Label l, string s)
    {
    if (InvokeRequired)
        {
        Invoke(new MethodInvoker(delegate { SetLabelText(l, s); }));
        }
    else
        {
        l.Text = s;
        }
    }
 
Share this answer
 
Comments
Member 8404471 13-Apr-12 9:34am    
Thanks!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900