Solved

WinForms Application TextBox.Text = LARGE_TEXT causes the UI to update slowly. How can I improve performance?

Posted on 2009-07-06
8
245 Views
Last Modified: 2012-08-13
When the application tries to read a 5MB text file and assign the value to a TextBox control, it takes 3-4 seconds for the UI to update. Is there a way to improve performance?
Output.Text = TEXT_FROM_LARGE_FILE

Open in new window

0
Comment
Question by:proaceteam
  • 4
  • 2
  • 2
8 Comments
 
LVL 6

Expert Comment

by:ahmad2121
ID: 24790003
You could use a loop and use substring to gradually build the string.

I would profile the situation. Basically, figure out how many characters you can put lag free (let's say its 1000), then inside your for loop, increment that substring and append it to Output.Text.

Don't forget to:

Application.DoEvents()

in your loop to allow other things to happen while it updates! You have have to introduce a few ms delay in the loop as well.
0
 
LVL 6

Expert Comment

by:ahmad2121
ID: 24790036
0
 

Author Comment

by:proaceteam
ID: 24790049
Some additional code to give you an idea of what is going on right now.


Dim srReadLog As New StreamReader(strOutputPath + "\" + Path.GetFileName(strOutputFileName))

Dim sb As New StringBuilder()

sb.Append(srReadLog.ReadToEnd())

OutputText.Visible = True

OutputText.Text = sb.ToString()

srReadLog.Close()

Open in new window

0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24790596
Try putting the retrieve text directly to the textbox and not through stringbuilder.
Dim srReadLog As New StreamReader(strOutputPath + "\" + Path.GetFileName(strOutputFileName))

OutputText.Visible = True

OutputText.Text = srReadLog.ReadToEnd()

srReadLog.Close()

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 6

Expert Comment

by:ahmad2121
ID: 24790834
Which I imagine is the exact same thing...
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24790950
@ahmad2121

>>Which I imagine is the exact same thing...

Nope...

If you can see... you put the contents of the file in the stringbuilder which would took approximately some seconds to do then you put that again in the textbox which will take some second again to do...

StringBuilder here is no help at all, since you where not appending or concatenate strings.

By directly putting the contents of the textfile to the textbox you remove the fraction of seconds in filling up the values in the stringbuilder.

:)

0
 
LVL 6

Accepted Solution

by:
ahmad2121 earned 500 total points
ID: 24796808
It is not even a fraction of a second. And if the compiler could optimize it away, then the argument is invalid.

Here is a better solution than the one I initially mentioned.

Here I use a bit of trickery. I know that all the parameters being passed to new string will get evaluated before the new string is created, meaning that temp will get populated.

Change count to whatever is more convenient. The bigger the count, the smaller the load time the more "laggy" it is. The smaller the count, the more snappier it is but it will take a bit longer.
        Dim srReadLog As New StreamReader(strOutputPath + "\" + Path.GetFileName(strOutputFileName))

        OutputText.Visible = True

        Dim count As Integer

        count = 40000

        Dim temp(count) As Char

        Do While srReadLog.Peek() >= 0

            OutputText.AppendText(New String(temp, 0, srReadLog.Read(temp, 0, temp.Length)))

            Application.DoEvents()

        Loop

        srReadLog.Close()

Open in new window

0
 

Author Closing Comment

by:proaceteam
ID: 31600402
Thanks a lot Ahmad. That worked beautifully!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now