| | |
VB6 application form freezes
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2005
Posts: 107
Reputation:
Solved Threads: 3
Hi.
I'm a long time asp-developer, but fairly new to VB so I don't know all the trick yet.
My problem:
I'm writing a program that fetches file and directory information from a given drive/location and stores information in an array for further processing. During this procedure the application sometimes freezes and becomes "Not responding". At the end of the function loop, though, it unfreezes and goes back to normal - it seems the freezing occurs when the function becomes too processor intensive.
Sample code:
So...how do I make the function loop run without freezing the form (and the progressbar), so the user doesn't think the program crashed??
madmital
I'm a long time asp-developer, but fairly new to VB so I don't know all the trick yet.
My problem:
I'm writing a program that fetches file and directory information from a given drive/location and stores information in an array for further processing. During this procedure the application sometimes freezes and becomes "Not responding". At the end of the function loop, though, it unfreezes and goes back to normal - it seems the freezing occurs when the function becomes too processor intensive.
Sample code:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Private Sub makeTree(Folder) ProgressBar1.Visible = True Dim f, Files, SubFolders, fsize, intFl, intStartSearchbs, intLevel Set f = fso.GetFolder(Folder) Set Files = f.Files intFl = 1 intStartSearchbs = 1 If f.Size / 1024 > 1024 Then fsize = Round((f.Size / 1024 / 1024), 1) Else fsize = "0" End If Do While Not InStr(intStartSearchbs, Replace(Folder, TestDrive & ":\", ""), "\", 1) = 0 intStartSearchbs = InStr(intStartSearchbs, Replace(Folder, TestDrive & ":\", ""), "\", 1) + 1 intFl = intFl + 1 Loop intLevel = intFl + 1 filenow = filenow + 1 tree_n(filenow - 1, 1) = Right(Folder, Len(Folder) - InStrRev(Folder, "\", , vbTextCompare)) tree_n(filenow - 1, 2) = fsize tree_n(filenow - 1, 3) = intFl tree_n(filenow - 1, 4) = 1 intFl = 1 If Files.Count > 0 Then For Each File In Files intStartSearchbs = 1 intFl = 0 If File.Size / 1024 > 1024 Then fsize = Round((File.Size / 1024 / 1024), 1) Else fsize = "0" End If If intLevel = "" Then intLevel = 1 filenow = filenow + 1 tree_n(filenow - 1, 1) = Replace(File, TestDrive & ":\", "") tree_n(filenow - 1, 2) = fsize tree_n(filenow - 1, 3) = intLevel moveOn: intProgress = Int(100 - Int((filecount - filenow) / filecount * 100)) ProgressBar1.Value = intProgress currentAction.Refresh ProgressBar1.Refresh End If Next End If intFl = 1 Set SubFolders = f.SubFolders If SubFolders.Count > 0 Then For Each SubFolder In SubFolders makeTree (SubFolder.Path) Next End If End Sub
So...how do I make the function loop run without freezing the form (and the progressbar), so the user doesn't think the program crashed??
madmital
try use DoEvents , this function will provide a few processing resource for the rest of the system tasks such as screen update.
here is Example
Second Example
here is Example
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
For i = 0 To 100 DoEvents Next i
Second Example
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Do Until something DoEvents Loop
•
•
Join Date: Jun 2005
Posts: 107
Reputation:
Solved Threads: 3
In the words of Montgomery Burns: "Excellent...."
It works like a charm just by inserting 'DoEvents'.
The program doesn't freeze anymore, and I'm able to move the application window around the desktop without any problems (this wasn't possible earlier either).
Furthermore this also takes care of another problem I had with a 'Cancel' button that didn't work.
For other newbies reading this thread:
The file/folder structure I use for testing holds 2795 files and folders.
Before adding 'DoEvents' all needed information was gathered from my test structure in 16 sec.
After adding 'DoEvents' the exact same process took 19 sec (approx. 19% slower).
So it seems 'DoEvents' lowers performance.
But....it works! So I'm a happy dude today :-)
Thank you 'invisal' and thank you 'Comatose'
It works like a charm just by inserting 'DoEvents'.
The program doesn't freeze anymore, and I'm able to move the application window around the desktop without any problems (this wasn't possible earlier either).
Furthermore this also takes care of another problem I had with a 'Cancel' button that didn't work.
For other newbies reading this thread:
The file/folder structure I use for testing holds 2795 files and folders.
Before adding 'DoEvents' all needed information was gathered from my test structure in 16 sec.
After adding 'DoEvents' the exact same process took 19 sec (approx. 19% slower).
So it seems 'DoEvents' lowers performance.
But....it works! So I'm a happy dude today :-)
Thank you 'invisal' and thank you 'Comatose'
We Can Fix That Also, By using a quick API call, and only calling doevents if there is information waiting in the message queue for our app (since I can see you're concerned with speed
) In your declarations section of your form, OR in a standard code module, add this (assuming code module):
Then Just Replace the Lonely DoEvents With This Code:
Let me know the speed difference if you would please
) In your declarations section of your form, OR in a standard code module, add this (assuming code module): Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Public Declare Function GetInputState Lib "user32" () As Long
Then Just Replace the Lonely DoEvents With This Code:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
If GetInputState <> 0 Then DoEvents
Let me know the speed difference if you would please
•
•
Join Date: Jun 2005
Posts: 107
Reputation:
Solved Threads: 3
•
•
•
•
since I can see you're concerned with speed
Well, (Comatose) adding your code instead of just 'DoEvents' actually increases performance by about 10%, but it seems the 'freezing problem' returns in a slightly different fashion.
The program will freeze only on some run-throughs (using the exact same test data) if I leave the program alone during the process. But I found that if I, say, move the application window around during the process - thus pausing the code/process - it does not freeze or lock up(?).
Maybe I'll just stick with the lonely 'DoEvents' since my initial problem is now taken of and the code does run pretty fast.
Cool, I know why it does what it does, and it's because it only responds to user initiated events (getinputstate), It just makes it so that the user can interact with it... I wasn't sure if the speed increase was significant enough to warrant your use of it.... I don't personally use it, unless the application does an extreme amount of processing or calculations. I stick with the standard doevents... thanx for letting me know how it worked (or didn't in this case).
![]() |
Similar Threads
- Javascript/VBScript within VB6 application (Visual Basic 4 / 5 / 6)
- Error while running VB6 application with Oracle on Windows XP SP2 (Visual Basic 4 / 5 / 6)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Argument not optional error
- Next Thread: Adding Horizondal Scroll bar to Combo Box
Views: 7551 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Visual Basic 4 / 5 / 6
* 6 429 2007 access activex add age append application basic beginner birth bmp c++ calculator cd cells.find click client code college column component connection copy creat ctrl+f data database datareport date delete dissertations dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver struct subroutine table tags textbox time timer urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






