c# - making a .NET TextBox work FIFO style -
i have win gui application written in c#, in have textbox
component write log to. @ point gets loaded , entire app starting falter. i'd make efficient mechanism make fifo, meaning - make fixed size , deleting oldest content automatically.
is there .net/c# feature doing that? otherwise, right way this?
update: have issue other sort of textual data, , not logs. therefor, listbox
not proper solution me.
to create circular buffer text, i'd use stringbuilder
, capacity set twice amount of data want display.
const int displaysize = 10000; stringbuilder fifo = new stringbuilder(2 * displaysize); string appendtofifo( string s ) { if (s.length >= displaysize) { // fact: display include data s // therefore, toss entire buffer, , keep tail of s fifo.clear(); fifo.append(s, s.length - displaysize, displaysize); return fifo.tostring(); } if (fifo.length + s.length > fifo.capacity) { // fact: overflow fifo // therefore, keep data in fifo remains on display fifo.remove(0, fifo.length + s.length - displaysize); } fifo.append(s); if (fifo.length <= displaysize) { // fact: entire fifo content fits on display // therefore, send return fifo.tostring(); } // fact: fifo content exceed display size // therefore, extract tail return fifo.tostring(fifo.length - displaysize, displaysize); }
the fast path, when none of if conditions true, avoids unnecessary copies (in .net world strings immutable, final copy create output string cannot avoided). , in other cases needed characters copied. increasing capacity of buffer improve utilization fraction of fast path. i've been careful avoid doing creating string object old content remains on display, has no purpose except concatenate new content, , becomes garbage.
obviously, if use p/invoke pass pointer stringbuffer's content instead of copying out substring, more efficient.
Comments
Post a Comment