I am writing a new WPF control application from a scratch based on a winForm version. The control will monitor a machine over serial. Within the control I want to have a text window that will monitor the input data coming from the machine.
For this I have a TextBlock control. My concern is that as this runs constantly that the TextBlock will might cause memory issues as the stored data grows. The control can run for months without restart.
I haven't had any search luck on limiting TextBlock memory and handling of old information.
Is there something I should do to keep the block from infinitely storing data and causing problems down the line? Or is there a better control to display and monitor incoming data from the machine?
I havent written the code for this yet as I want to start it right. However, when I did this with winform I used a TextBox. There I had an event handler detect incoming data and use stringbuilder to
textbox.AppendText(Environment.newline + string)
the received data to the window. I thought TextBlock might be a better way to go this round.
You can use a StringBuilder
to create a string
that you then set the Text
property of the TextBlock
to. Any old Text
value will then be eligible for garbage collection. There is no way and there should be no reason to "limiting TextBlock memory and handling of old information".
Or is there a better control to display and monitor incoming data from the machine?
Whether you use a TextBlock
or a TextBox
shouldn't really matter as far as memory and performance are concerned. If you require the text to be selected, a TextBox
is a better choice. Otherwise, you should choose a TextBlock
. There is no more lighweight way to display text in a WPF application.