Results 1 to 10 of 10

Thread: [RESOLVED] Long Loops freeze my program

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    27

    Resolved [RESOLVED] Long Loops freeze my program

    If a Loop is very long (lasts more than 10 seconds or so) the program just stops responding and I have to use ctrl-alt-delete. Also, while the Loop is running, I can't use my computer until it either finishes or until it stops responding. How do I get my program to not work so hard?
    VB Code:
    1. Dim lASDF As Long
    2.  
    3. Private Sub Form_Load()
    4. End Sub
    5.  
    6. Private Sub cmndStart_Click()
    7.  
    8.     lASDF = 0
    9.  
    10.     Do
    11.         lASDF = lASDF + 1
    12.         lblCount.Caption = lASDF
    13.     Loop Until (1 = 2)
    14.  
    15. End Sub
    If the Loop condition is 'Until (lASDF = 1000000)' it will take about nine seconds to finish. Looping a million times isn't enough to freeze the program, but during the time it takes I can't do anything on my computer. Not even view my desktop.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Long Loops freeze my program

    You need to manually relinquish some CPU control, otherwise your loop makes continous control requests.

    You can either

    a) chuck a DoEvents in there,

    b) chuck a Sleep() call in there which pauses your thread, or

    c) both.

    The problem with DoEvents is that while it is quite effective, it can cause a jumble with code jumping about within your app (especially if you have lots of events firing, they will go out of order) and it is pretty resource-intensive itself. So try this:

    VB Code:
    1. ' In a module, or Private in a class/form/uc
    2. Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    3.  
    4. ' in your loop body
    5. Sleep 10

    That should be enough to reduce the CPU load a fair bit. You can play with the pause duration to see which has best effect.

    Alternatively, you can also go a rather more hardcore route and do this (A combination of DoEvents, without the mess, and Sleep without the unnecessary pause):
    VB Code:
    1. Type Msg
    2.     hWnd As Long
    3.     message As Long
    4.     wParam As Long
    5.     lParam As Long
    6.     time As Long
    7.     pt As Double
    8. End Type
    9.  
    10. Declare Function GetMessage Lib "user32" Alias "GetMessageW" ( _
    11.     ByRef lpMsg As MSG, _
    12.     ByVal hWnd As Long, _
    13.     ByVal wMsgFilterMin As Long, _
    14.     ByVal wMsgFilterMax As Long _
    15. ) As Long
    16.  
    17. Declare Function TranslateMessage Lib "user32" ( _
    18.     ByRef lpMsg As MSG _
    19. ) As Long
    20.  
    21. Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageW" ( _
    22.     ByRef lpMsg As MSG _
    23. ) As Long
    24.  
    25. ' Usage (as a function replacement for DoEvents)
    26. Sub TwiddleThumbs()
    27. Dim mMsg As MSG
    28.     Do While (GetMessage(mMsg, 0&, 0&, &H400)
    29.         TranslateMessage mMsg
    30.         DispatchMessage mMsg
    31.     Loop
    32. End Sub

    (I hope that works, I didn't test it)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    27

    Re: Long Loops freeze my program

    I tried just sleep. Even if sleep is set to 500, the program still freezes. I then tried the program you made and I put my 'lblCount.Caption = lASDF' in there. Not sure if I made a mistake, but I got no error messages (after a few things were changed). BUT it still froze. So I added in sleep 500, and that didn't help either. Here is what I last tried, but it still froze after 10 or so seconds:
    VB Code:
    1. Dim lASDF As Long
    2.  
    3. Private Type Msg
    4.     hWnd As Long
    5.     message As Long
    6.     wParam As Long
    7.     lParam As Long
    8.     time As Long
    9.     pt As Double
    10. End Type
    11.  
    12. Private Declare Function GetMessage Lib "user32" Alias _
    13.     "GetMessageW" ( _
    14.     ByRef lpMsg As Msg, _
    15.     ByVal hWnd As Long, _
    16.     ByVal wMsgFilterMin As Long, _
    17.     ByVal wMsgFilterMax As Long _
    18. ) As Long
    19.  
    20. Private Declare Function TranslateMessage Lib "user32" ( _
    21.     ByRef lpMsg As Msg _
    22. ) As Long
    23.  
    24. Private Declare Function DispatchMessage Lib "user32" Alias _
    25.     "DispatchMessageW" ( _
    26.     ByRef lpMsg As Msg _
    27. ) As Long
    28.  
    29. Private Declare Sub Sleep Lib "kernel32" _
    30.     (ByVal dwMilliseconds As Long)
    31.  
    32. Public Sub cmndStart_Click()
    33.  
    34. Dim mMsg As Msg
    35.  
    36. lASDF = 0
    37.  
    38.     Do While (GetMessage(mMsg, 0&, 0&, &H400))
    39.         TranslateMessage mMsg
    40.         DispatchMessage mMsg
    41.         lASDF = lASDF + 1
    42.         Sleep 500
    43.         lblCount.Caption = lASDF
    44.     Loop
    45.  
    46. End Sub
    Last edited by ship; Sep 22nd, 2005 at 07:35 AM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    27

    Re: Long Loops freeze my program

    Oops. Realized I made a mistake, and edited my above post to reflect the correction.

    It doesn't freeze now. That's good. But 1)my label never reads anything, and 2) if 'Do While (GetMessage(mMsg, 0&, 0&, &H400))' then how do I have the loop stop when a condition of my choosing is met?

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Long Loops freeze my program

    put in a DoEvents after
    lblCount.Caption = lASDF

    VB Code:
    1. Do While (GetMessage(mMsg, 0&, 0&, &H400))
    2.         TranslateMessage mMsg
    3.         DispatchMessage mMsg
    4.         lASDF = lASDF + 1
    5.         Sleep 500
    6.         lblCount.Caption = lASDF
    7.         Doevents
    8.     Loop
    Last edited by Static; Sep 22nd, 2005 at 08:11 AM.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Long Loops freeze my program

    DoEvents will return control to the OS for whatever the OS needs to do, but Sleep will just stop everything for a specified period of time.

    DoEvents would be the way to go.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    27

    Re: Long Loops freeze my program

    Ok, ty. I'll try that.

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Long Loops freeze my program

    The GetMessage loop I provided you is supposed to go inside your main loop. It is a direct replacement for DoEvents, because Doevents can get very messy sometimes.

  9. #9
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: [RESOLVED] Long Loops freeze my program

    sorry, but how do you exit that do loop?

  10. #10
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: [RESOLVED] Long Loops freeze my program

    This loop makes the application stop executing .. what's the proper way to put this loop in a loop?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width