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:

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

For i = 0 To 100
   DoEvents
Next i

Second Example

Do Until something
   DoEvents
Loop

I think Visal certainly hit the nail on the head with that one. Doevents is the way to go. Do me one big favor, and let me know how it turns out.

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'

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):

Public Declare Function GetInputState Lib "user32" () As Long

Then Just Replace the Lonely DoEvents With This Code:

If GetInputState <> 0 Then DoEvents

Let me know the speed difference if you would please :)

since I can see you're concerned with speed

isn't everyone? ;)

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). :)

hmmm...."Smarter than the average bear, Bobo" :p
Great...thank you for your help!

This question has already been answered. Start a new discussion instead.