c# - Prevent a text box from lagging due to fast updates -



c# - Prevent a text box from lagging due to fast updates -

given next illustration code:

new thread(() => { for(int = 0; < 10000; i++) { invoke((methodinvoker)() => { mytextbox.text += datetime.now.tostring() + "\r\n"; mytextbox.selectedindex = mytextbox.text.length; mytextbox.scrolltocarat(); }); } }).start();

when run code, after loop , thread terminate, text box still updating (presumably because of buffered invokes). application uses similar logic fill text box, , i'm having same problem.

my question is: how can fill text box fast possible, still scroll bottom every time, , yet reduce/eliminate lag?

there few options can take here. first, can set double buffering on form, end drawing updates on underlying bitmap, displays newly drawn image (instead of individually drawing controls on graphics object). saw 50% speed increment method. throw constructor:

this.setstyle( controlstyles.allpaintinginwmpaint | controlstyles.userpaint | controlstyles.doublebuffer,true);

the other thing maintain in mind string concatenation slow big amounts of data. you're improve off using stringbuilder build info , show using stringbuilder.tostring (although still improve stagger updates, maybe 1 time every 100 iterations). on machine, changing append stringbuilder, went 2.5 minutes run through 10k iterations 1.5 minute. better, still slow.

new system.threading.thread(() => { for(int = 0; < 10000; i++) { sb.appendline(datetime.now.tostring()); invoke((action)(() => { txtarea.text = sb.tostring(); txtarea.selectionstart = txtarea.text.length; txtarea.scrolltocaret(); })); } }).start();

finally, tested out staggering (threw single conditional above code, right before invoke call), , finished in 2 seconds. since we're using stringbuilder build string, still retain data, have updates 100 times opposed 10k times.

so now, options? given winform application, can utilize 1 of many timer objects perform ui update particular control, or can maintain counter of how many "reads" or "updates" underlying info (in case, stream) , update ui on x number of changes. utilizing both stringbuilder alternative , staggered updates way go.

c# winforms

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -