Lenci
Lenci

Reputation: 122

Slow VB6 String Operations on Windows 7

We have a VB6 program that does some string processing in a loop (approximately 500 times) and appends info to a textbox. Each iteration on the loop includes basic operations like Trim, Len, Left, Mid, etc, and finally appends the string to a textbox (the whole form is still invisible at this point). Finally, after the loop, the code calls Show on the form.

On Windows XP, these 500 loops take about 4 seconds. On Windows 7, the exact same code runs in about 90 seconds.

Any suggestions on how to fix this?

Thanks.

Upvotes: 2

Views: 1263

Answers (3)

George
George

Reputation: 2213

I'm guessing you append the text box on each loop iteration... If you can, store everything in a variable and append it to the TextBox once after the loop is finished. Displaying text in a text box takes a lot of time in VB6.

EDIT: After further investigation and testing, I came to a conclusion that performance on directly assigning strings to the Text property of a TextBox degrades dramatically when the length of the control reaches maximum. The maximum on my PC is 65535 for some reason, even though according to MSDN its

Windows NT 4.0, Windows 2000, Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: For single line text box controls, if the MaxLength property is set to 0, the maximum number of characters the user can enter is 2147483646 or an amount based on available memory, whichever is smaller

Basically what seems to be happening, if you keep adding text to the TextBox each iteration, it isn't that slow until you reach maximum. What's even more puzzling, when you try to add text beyond the maximum, there will be no errors, but performance degrades significantly.

In my test loop I go from 0 to 12773 I have this:

Text2.Text = Text2.Text + CStr(a) + " "

So when the loop is completed in 4 seconds, the Text2.Text is 65534 characters long. Now, when I double the loop to go beyond the maximum allowed length of the TextBox, it takes three times as much time to complete it.

12773 - 4 seconds

12773*2 - 16 seconds

After realizing this, my first thought was to replace the TextBox with a RichTextBox. But the performance of the latter is even worse. This is assuming you update it every iteration.

It seems that you are stuck with a dilemma - suffer slow performance or change the code to update text box only once after the loop is completed. Further, due to TextBox's maximum length limit, I recommend switching to a RichTextBox or depending on the purpose of this - some other object.

I hope my findings are helpful - it has certainly been fun finding out all these little programming quirks.

Upvotes: 4

Mark Bertenshaw
Mark Bertenshaw

Reputation: 5689

I would recommend that you find out exactly what is slow. Redo your timings before and after the string concatenation, and then before and after the copy of the string into the text box. Have the Ole Automation string operations somehow become slower, or has the copying of text into a VB text box become slower?

Once you know this, we can continue with phase 2 ... :-)

Upvotes: 0

MarkJ
MarkJ

Reputation: 30398

Try LockWindowUpdate to switch off updating for your form.

 Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As Long) As Long 

'To turn it on just call it like this, passing it the hWnd of the window to lock. 
LockWindowUpdate Form1.hWnd

'intensive updating here

 'to turn it off just call it and pass it a zero.
 LockWindowUpdate 0

From here

Upvotes: 2

Related Questions