Solved

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

Posted on 2009-07-06
8
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

756 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