Solved

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

Posted on 2009-07-06
8
242 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

19 Experts available now in Live!

Get 1:1 Help Now