• C# TextBox控件 显示大量数据


           串口通信:在使用TextBox空间显示数据时,因为要显示大量的接收到的数据,当数据量大且快速(串口1ms发送一条数据)时,使用+=的方式仍然会造成界面的卡顿(已使用多线程处理),但使用AppendText效果就会好一点。

           代码:

        

     1  Thread dataThread = new Thread(delegate()
     2            {
     3                while (threadFlag)
     4                {
     5                    if (Port != null && Port.IsOpen)
     6                    {
     7                        length = Port.BytesToRead;
     8                        if (length > 0)
     9                        {
    10 
    11                            bytes = new byte[length];
    12                            Port.Read(bytes, 0, length);
    13                           
    14                            tcp.SendData(bytes);
    15                           
    16                            if (textBox.InvokeRequired)
    17                            {
    18 
    19                                textBox.Invoke(new Action<string>(s =>
    20                                {
    21                                    textBox.AppendText(s + Environment.NewLine);
    22                                    textBox.SelectionStart = textBox.TextLength;
    23                                    textBox.ScrollToCaret();
    24 
    25                                }), System.Text.Encoding.ASCII.GetString(bytes));
    26 
    27                            }
    28                            else
    29                            {
    30                                textBox.AppendText(System.Text.Encoding.ASCII.GetString(bytes) + Environment.NewLine);
    31                                textBox.SelectionStart = textBox.TextLength;
    32                                textBox.ScrollToCaret();
    33 
    34                            }
    35                        }
    36 
    37                    }
    38                    Thread.Sleep(1);
    39                }
    40 
    41            });
    42            dataThread.IsBackground = true;
    43            dataThread.Name = PortName;
    44            dataThread.Start();
    45        }
    46 
    47    }
  • 相关阅读:
    Leetcode 242.有效的字母异位词 By Python
    Leetcode 344.反转字符串 By Python
    Leetcode 217.存在重复元素 By Python
    js 动态加载select触发事件
    MUI 里js动态添加数字输入框后,增加、减少按钮无效
    【 jquery 】常用
    MySql 常用语句
    CSS 选择器 知识点
    HTML 符号实体
    log4net 配置
  • 原文地址:https://www.cnblogs.com/zhaoyihao/p/4533125.html
Copyright © 2020-2023  润新知