My problem: I have DataReceived event for SerialPort, which listens to serial port and appends everything to RichTextBox. Now I’d like to save RichTextBox’s content to txt file. I could do this:
string text = RichTextBox.Text;
// save file
RichTextBox.Clear();
But I fear (and please correct me if I am wrong), that just between my two actions (save content to variable, clear RichTextBox) another input could come to SerialPort. Because I didn’t saved it to my variable and I clear RichTextBox right after it, this new data could be deleted, without me even knowing that it ever existed.
Could this really happen and how can I avoid it?
Thanks
You have to synchronize your Inputs from the Serial port somehow. I assume you use Invoke in the DataReceived event. So any changes to the contents of the RichTextBox are done by the main thread and you should be safe.
But as the stream from the serial port is unlikely to be RTF (you would have other problems if it was), you might as well use a TextBox (MultLine=true).