Question

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

Was it helpful?

Solution

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).

OTHER TIPS

This could happen if you are using multiple threads. In a single thread application, this problem would not occur.

To prevent this, you can use mutex. This is how it works: The code snippets that work on the contents of RichTextBox should not be executed concurrently. These code snippets are called critical sections. When you enter a critical section, you lock the mutex and release it when exiting the critical section. If a mutex is locked when you try to enter a critical section, wait until that mutex is released, and then do your job.

This way, if any input comes through the serial port while writing to the file, the thread recieving the data will wait until the file I/O is finished and then append it to the RichTextBox.

You can achieve this by using the lock statement in c#.

Further reading: Mutex, Critical Section

Absolutely that could happen (and probably would).

Unless there are other considerations, I believe that you would be better served using the data from the SerialPort stream to write the text file directly, rather than writing to the file using the RichTextBox.Text. The DataReceived event handler should look something like this...

private void SerialPort1.DataReceived(object sender, SerialDataReceivedEventArgs e)
{
   string DataText = // code to read the serialport data and convert 
   //it into a string, which you already have so I 
   //won't repeat it here.

   WriteToTextBox(DataText);
   WriteToFile(DataText);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top