Solved

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

Posted on 2009-07-06
8
260 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

726 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