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: madmital is an unknown quantity at this point 
Solved Threads: 3
madmital madmital is offline Offline
Junior Poster

VB6 application form freezes

 
0
  #1
Jun 1st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: VB6 application form freezes

 
0
  #2
Jun 1st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: VB6 application form freezes

 
0
  #3
Jun 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 107
Reputation: madmital is an unknown quantity at this point 
Solved Threads: 3
madmital madmital is offline Offline
Junior Poster

Re: VB6 application form freezes

 
0
  #4
Jun 1st, 2005
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'
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: VB6 application form freezes

 
0
  #5
Jun 1st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 107
Reputation: madmital is an unknown quantity at this point 
Solved Threads: 3
madmital madmital is offline Offline
Junior Poster

Re: VB6 application form freezes

 
0
  #6
Jun 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: VB6 application form freezes

 
0
  #7
Jun 1st, 2005
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).
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 107
Reputation: madmital is an unknown quantity at this point 
Solved Threads: 3
madmital madmital is offline Offline
Junior Poster

Re: VB6 application form freezes

 
0
  #8
Jun 2nd, 2005
hmmm...."Smarter than the average bear, Bobo" :p
Great...thank you for your help!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum


Views: 7551 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC