C#交叉线程消息传递错误中的Com端口通信 [英] Com port communication in C# cross thread messaging error

查看:127
本文介绍了C#交叉线程消息传递错误中的Com端口通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我编写了一个从com端口读取数据并进行进一步处理的程序。程序运行良好但有时它在为文本框分配值时抛出crossthreadmessaging异常。我需要你宝贵的指导来解决这个问题。

下面是我在我的申请中使用的代码。



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.

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

推荐答案

这很奇怪 - 您只访问委托中始终被调用的文本框,因此异常不应该正在发生。在代表中,你正在捕捉异常,所以你不应该看到它们。



但是有几件事要尝试改善这种情况。

1)你不需要InvokeRequired测试:DataReceived总是在一个单独的线程上,它永远不在UI线程上 - 参见文档: SerialPort.DataReceived Event(System.IO.Ports) [ ^ ]

2)在当前线程中进行所有解析,并使UI只执行可调用代码 - 这样您的UI应该保持更快的响应,因为如果有很多工作,那么最小化它的工作要做。



所以试试这样:

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:
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.


这篇关于C#交叉线程消息传递错误中的Com端口通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆