码迷,mamicode.com
首页 > Windows程序 > 详细

C# TextBox控件 显示大量数据

时间:2015-05-27 13:41:02      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:

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

       代码:

    

public void DataReceThread(System.Windows.Forms.TextBox textBox, Tcp tcp)
{
byte[] bytes;
int length = 0;

Thread dataThread = new Thread(delegate()
{
while (threadFlag)
{
if (Port != null && Port.IsOpen)
{
length = Port.BytesToRead;
if (length > 0)
{

bytes = new byte[length];
Port.Read(bytes, 0, length);

tcp.SendData(bytes);

if (textBox.InvokeRequired)
{

textBox.Invoke(new Action<string>(s =>
{
textBox.AppendText(s + Environment.NewLine);
textBox.SelectionStart = textBox.TextLength;
textBox.ScrollToCaret();

}), System.Text.Encoding.ASCII.GetString(bytes));

}
else
{
textBox.AppendText(System.Text.Encoding.ASCII.GetString(bytes) + Environment.NewLine);
textBox.SelectionStart = textBox.TextLength;
textBox.ScrollToCaret();

}
}

}

}

});
dataThread.IsBackground = true;
dataThread.Name = PortName;
dataThread.Start();
}

}

C# TextBox控件 显示大量数据

标签:

原文地址:http://www.cnblogs.com/zhaoyihao/p/4533125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!