?
Solved

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

Posted on 2009-07-06
8
Medium Priority
?
272 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
Independent Software Vendors: 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 …
Suggested Courses

770 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