944,092 Members | Top Members by Rank

Ad:
Jun 1st, 2005
0

VB6 application form freezes

Expand Post »
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:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub makeTree(Folder)
  2. ProgressBar1.Visible = True
  3. Dim f, Files, SubFolders, fsize, intFl, intStartSearchbs, intLevel
  4. Set f = fso.GetFolder(Folder)
  5. Set Files = f.Files
  6. intFl = 1
  7. intStartSearchbs = 1
  8.  
  9. If f.Size / 1024 > 1024 Then
  10. fsize = Round((f.Size / 1024 / 1024), 1)
  11. Else
  12. fsize = "0"
  13. End If
  14.  
  15. Do While Not InStr(intStartSearchbs, Replace(Folder, TestDrive & ":\", ""), "\", 1) = 0
  16. intStartSearchbs = InStr(intStartSearchbs, Replace(Folder, TestDrive & ":\", ""), "\", 1) + 1
  17. intFl = intFl + 1
  18. Loop
  19. intLevel = intFl + 1
  20. filenow = filenow + 1
  21. tree_n(filenow - 1, 1) = Right(Folder, Len(Folder) - InStrRev(Folder, "\", , vbTextCompare))
  22. tree_n(filenow - 1, 2) = fsize
  23. tree_n(filenow - 1, 3) = intFl
  24. tree_n(filenow - 1, 4) = 1
  25. intFl = 1
  26.  
  27. If Files.Count > 0 Then
  28. For Each File In Files
  29. intStartSearchbs = 1
  30. intFl = 0
  31. If File.Size / 1024 > 1024 Then
  32. fsize = Round((File.Size / 1024 / 1024), 1)
  33. Else
  34. fsize = "0"
  35. End If
  36.  
  37. If intLevel = "" Then intLevel = 1
  38. filenow = filenow + 1
  39. tree_n(filenow - 1, 1) = Replace(File, TestDrive & ":\", "")
  40. tree_n(filenow - 1, 2) = fsize
  41. tree_n(filenow - 1, 3) = intLevel
  42.  
  43.  
  44. moveOn:
  45. intProgress = Int(100 - Int((filecount - filenow) / filecount * 100))
  46. ProgressBar1.Value = intProgress
  47. currentAction.Refresh
  48. ProgressBar1.Refresh
  49. End If
  50. Next
  51. End If
  52. intFl = 1
  53. Set SubFolders = f.SubFolders
  54. If SubFolders.Count > 0 Then
  55. For Each SubFolder In SubFolders
  56. makeTree (SubFolder.Path)
  57. Next
  58. End If
  59. 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
Similar Threads
Reputation Points: 10
Solved Threads: 5
Junior Poster
madmital is offline Offline
119 posts
since Jun 2005
Jun 1st, 2005
0

Re: VB6 application form freezes

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
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. For i = 0 To 100
  2. DoEvents
  3. Next i

Second Example

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Do Until something
  2. DoEvents
  3. Loop
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jun 1st, 2005
0

Re: VB6 application form freezes

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 1st, 2005
0

Re: VB6 application form freezes

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'
Reputation Points: 10
Solved Threads: 5
Junior Poster
madmital is offline Offline
119 posts
since Jun 2005
Jun 1st, 2005
0

Re: VB6 application form freezes

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):
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 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)
  1. If GetInputState <> 0 Then DoEvents

Let me know the speed difference if you would please
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 1st, 2005
0

Re: VB6 application form freezes

Quote ...
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.
Reputation Points: 10
Solved Threads: 5
Junior Poster
madmital is offline Offline
119 posts
since Jun 2005
Jun 1st, 2005
0

Re: VB6 application form freezes

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).
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 2nd, 2005
0

Re: VB6 application form freezes

hmmm...."Smarter than the average bear, Bobo" :p
Great...thank you for your help!
Reputation Points: 10
Solved Threads: 5
Junior Poster
madmital is offline Offline
119 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Argument not optional error
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Adding Horizondal Scroll bar to Combo Box





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC