AppendTex problem about RichTextBox

The RichTextBox control need to save some data received from a serial port. At the same time, also need to process the KeyPress event. The problem is RichTextBox.AppendText method will lost some data from the serial port when processing the key press event at the same time. My code is follow:

private: System::Void sfRichTextBox_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
if(m_bInputChar){
if(sfPort->IsOpen) {
sfPort->Write(e->KeyChar.ToString());
e->Handled = true;
}
}
}

private: System::Void sfPort_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs ^ e) {
String ^lpBuf = sfPort->ReadExisting();
StreamIn(lpBuf);
}

private: System::Void StreamIn(String^ text) {
if(sfRichTextBox->InvokeRequired)
{
StreamInCallback^ lpStreamInCB = gcnew StreamInCallback(this,&SerialTool::SerialForm::StreamIn);
this->BeginInvoke(lpStreamInCB,gcnew array<String^,1> { text });
}
else
{
swReport->Write(text);
sfRichTextBox->AppendText(text);
if(sfRichTextBox->TextLength>sfRichTextBox->MaxLength/4)
{
int delLine = sfRichTextBox->GetLineFromCharIndex(sfRichTextBox->MaxLength/4);
sfRichTextBox->Select(0,sfRichTextBox->GetFirstCharIndexFromLine(delLine)-1);
swLog->Write(sfRichTextBox->SelectedText);
sfRichTextBox->SelectedText = "";
sfRichTextBox->Select(sfRichTextBox->TextLength,0);
sfRichTextBox->ScrollToCaret();
}
sfRichTextBox->HideSelection = false;
}
}




Answer this question

AppendTex problem about RichTextBox

  • mmickas

    I don't know whether DataReceived event is raised on the same thread with KeyPress Event or not, but the the RichTextBox control and SerialPort control are located on same Form control. I found that the sfPort->Write() is caused by other control not the RichTextBox control also has the same problem. Continuously AppendText cause data lost only at sfPort->Write() event happened at the same time. If no sfPort->Write event, contnuously AppendText is ok. sfPort_DataReceived and sfRichTextBox_KeyPress are private members of same Form class.

  • Chris Stewart - WUK

    Based upon the code snippet you've supplied; using InvokeRequired/BeginInvoke means all access to the RichTextBox is done on one thread.  So, there isn't a synchronization problem there.

    Will the call to sfPort->Write() cause the DataReceived event to be raised on the same thread

    The only other thing I can think of is one routine is leaving a selection behind that the other is inadvertently overwriting.  Maybe you can trace what the code thinks is adding to the rich text box and compare that with that you see in the rich text box.  The difference between the two may make the cause of the problem more apparent to you or someone else in the forums.



  • Darren_Melb_Aust

    You could try setting a class instance flag to a known value in the KeyPress handler, then checking that value in the DataReceived handler (then resetting it before exiting the KeyPress handler). My guess is the sfPort->Write() call is causing a DataReceived event to occur before it returns meaning StreamIn is called before you're finished processing a key (maybe overwriting something).

  • JasonDWilson

    The bug is fixed by setting the DiscardNull property of SerialPort control to TRUE. Thanks.

  • AppendTex problem about RichTextBox