Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I have written a program that reads data from com port and do further process. Program is running good but sometimes it is throwing "crossthreadmessaging" exception when i assign value to the textbox. I need your valuable guidance to sort out this problem.
below is the code that i am using in my application.

C#
void lobjCOM1_DataReceived(object sender, SerialDataReceivedEventArgs e)
      {

              string lsCOM1Val = lobjCOM1.ReadExisting();

              if (lsCOM1Val.Length > 8)
              {

                  if (lsCOM1Val.Substring(lsCOM1Val.Length - 8, 1) == "H")
                  {
                      if (this.txtC2.InvokeRequired)
                      {
                          this.txtC2.BeginInvoke((MethodInvoker)delegate()
                          {
                              try
                              {
                                  this.txtC2.Text = (Int64.Parse(lsCOM1Val.Substring(lsCOM1Val.Length - 6, 5)) / 1000).ToString() + "." + String.Format("{0:000}", (Int64.Parse(lsCOM1Val.Substring(lsCOM1Val.Length - 6, 5)) % 1000));
                              }
                              catch (FormatException ex)
                              {
                                  this.txtC2.Text = "99.999";
                              }
                          });
                      }


                  }
              }
      }


thanks in advance...

What I have tried:

I have tried the above code that works fine but sometimes it throws error which it should not be. I am unable to guess the possible reason
Posted
Updated 3-Nov-16 4:11am

1 solution

That's very odd - you are only accessing the text box within the delegate which is always invoked, so the exception shouldn't be happening. And within the delegate, you are catching exceptions so you shouldn't see them anyway.

But a couple of things to try and improve the situation.
1) You don't need a InvokeRequired test: DataReceived is always on a separate thread, it is never on the UI thread - see the documentation: SerialPort.DataReceived Event (System.IO.Ports)[^]
2) Do all your parsing in the current thread, and make the UI do just the "invocable" code - that way your UI should stay more responsive as you are minimising its work if there is a lot to do.

So try like this:
C#
void lobjCOM1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    string lsCOM1Val = lobjCOM1.ReadExisting();
    if (lsCOM1Val.Length > 8)
        {
        if (lsCOM1Val.Substring(lsCOM1Val.Length - 8, 1) == "H")
            {
            string value = "99.999";
            try
                {
                value = (Int64.Parse(lsCOM1Val.Substring(lsCOM1Val.Length - 6, 5)) / 1000).ToString() + "." + String.Format("{0:000}", (Int64.Parse(lsCOM1Val.Substring(lsCOM1Val.Length - 6, 5)) % 1000));
                }
            catch (FormatException ex) {}
            txtC2.BeginInvoke((MethodInvoker)delegate() { this.txtC2.Text = value; });
            }
        }
    }
Which should make it easier to debug any odd exceptions you do get.
 
Share this answer
 

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