I have an application which monitors some devices. I can send a string to the device, and receive text back from the device.
What I need is to make a log file (just .txt) which logs all data sent / received by the device. This should be displayed live in a the application.
Currently what I do is write the logs to a rich textbox and from there to a file. But I want to split the backend code (interfacing with the device and creating the log file) from the gui. This to make the communication interface with the device more portable.
As I am thinking through this process I face one issue: How to update the logs live in my application? I can think of this option: Make a timer and make the gui update on a time interval. but this does not feels terrible. It would be nice to have a buffer in between, and update the gui if the buffer has changed.
Any tips on how to do this correctly?
Maybe you could make your log content IObservable and make your UI subscribe to its changes?
Another option would be to implement INotifyPropertyChanged and and use a PropertyChangedEventHandler to be notified of changes.
Here’s a code snippet showing you how to do it with the NotifyPropertyChanged:
To register to the changes you simply do something like (in your UI constructor):
Note: make sure you properly remove and add again the handler if you change YourClassInstance
And then to update your UI, in your UI class again: