[英]C# application slows Down after 20-30 mins?
I have c# application which reads data from serial port. 我有C#应用程序,它从串行端口读取数据。 I have put serial read handler in timer with interval 1 second , because data coming every 1 second on timer I calling 我将串行读取处理程序放入间隔为1秒的计时器中,因为我调用的计时器中的数据每隔1秒就会出现一次
delegate void SetTextCallback(string text);
ReceivedText(serialPort1.ReadExisting());
I also showing received data in richtextbox just to check it getting proper data or not. 我还在richtextbox中显示接收到的数据,只是为了检查它是否获取正确的数据。 But after 15-20 mins application slows down wont even respond. 但是在15到20分钟后,应用程序变慢甚至都不会响应。
private void ReceivedText(string text)
{
if (this.rtbReceived.InvokeRequired)
{
SetTextCallback x = new SetTextCallback(ReceivedText);
this.Invoke(x, new object[] { (text) });
}
else
{
this.rtbReceived.Text += text;
serialdata = text;
if (serialdata.Length > 0 &&
serialdata.Length < 42 &&
serialdata.Contains("#") ||
serialdata.StartsWith(" #"))
{
serialdata.Trim();
splitdata = serialdata.Split(' ');
try
{
txtBathTemp.Text = splitdata[3];
txtBaroPressure.Text = splitdata[4];
double stemp = double.Parse(splitdata[5]);
txtSampleTemp.Text = (Math.Round(stemp, 2)).ToString();
}
catch (Exception EX)
{
MessageBox.Show(ex.Message);
}
}
}
}
There is a chance that this.rtbReceived.Text
grows up after some time. 一段时间后, this.rtbReceived.Text
会长大。 Even if it doesn't use huge amount of memory, constantly manipulating String
is not efficent. 即使不使用大量内存,不断操作String
也不起作用。 Have you considered using StringBuilder
instead? 您是否考虑过使用StringBuilder
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.