与其在字符到达时实时显示到 TMemo 中,不如先尝试将它们保存到内存缓冲区中,然后让一个短计时器定期将缓冲区复制到 TMemory 中,并在添加时使用Lines->BeginUpdate()
andLines->EndUpdate()
方法新文本。此外,1000 行很多,您可能必须开始删除旧行,因为稍后会添加新行。我通常一次将TMemo
控件限制为几百行。
更新:尝试这样的事情:
TMemoryStream *Buffer;
// serial port callback
void BytesReceived(void *Data, int Length)
{
Buffer->Position = Buffer->Size;
Buffer->WriteBuffer(Data, Length);
}
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
Buffer = new TMemoryStream;
}
__fastcall TForm1::~TForm1()
{
delete Buffer;
}
void __fastcall TForm1::TimerElapsed(TObject *Sender)
{
if (Buffer->Size > 0)
{
Memo1->Lines->BeginUpdate();
Memo1->SelStart = Memo1->GetTextLen();
Memo1->SelLength = 0;
Memo1->SelText = AnsiString((char*)Buffer->Memory, Buffer->Size);
Memo1->SelStart = Memo1->GetTextLen();
Memo1->Perform(EM_SCROLLCARET, 0, 0);
Memo1->Lines->EndUpdate();
Buffer->Clear();
}
}