Results 1 to 15 of 15

Thread: Why does DoEvents use 100% cpu? And whats an alternative?

Threaded View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    Why does DoEvents use 100% cpu? And whats an alternative?

    Do
    Doevents
    Loop until condition = true

    thats the code thats generally used to have the app perform a task 'synchronously' so that further code is not executed until something else is done.

    But the problem is, the loop causes 100% cpu usage until the condition is true, slowing all other open processes needlessly.

    An alternative is adding the sleep function to the loop

    Do
    Doevents
    Sleep 100
    Loop until condition = true

    but Sleep causes all executions to halt in the app

    so..
    Doevents: Lets the app execute the background function, but uses 100% cpu
    Sleep: Does not let the app execute the background function, uses 0% CPU
    Combination: App can execute things in the background while sleep is not being called, ~0% CPU, but not good enough.

    There HAS to be a better way to wait until something is done (background) before going on to the next line, without using doevents or sleep..

    Anyone know what that is?

    the best way i can think of doing this is a timer.. but its not elegant at all
    VB Code:
    1. 'below is an alternative to RunSomeFunctionInTheBackground(): Do: Doevents: Loop until condition = true: Continue with other stuff
    2.  
    3. Private Sub Form_Load()
    4. mySub 0
    5. End Sub
    6.  
    7. Private Sub mySub(whereToStart As Integer)
    8. Select Case whereToStart
    9. Case 0
    10.     RunSomeFunctionInTheBackground()
    11.     Timer1.Enabled = True
    12.     Exit Sub
    13. Case 1
    14.     'The function is done running, I can continue with my app
    15. End Select
    16. End Sub
    17.  
    18. Private Sub Timer1_Timer()
    19. If someCondition = True Then
    20.     Timer1.Enabled = False
    21.     mySub 1
    22. End If
    23. End Sub
    24.  
    25. 'this method will function like DoEvents, and it will not halt execution of the thread like Sleep.  Timer interval is something like 10
    Basically I just want to be able to use

    VB Code:
    1. RunSomeFunctionInTheBackground()
    2. waitUntilThatFunctionisDone() '(but not use 100% cpu like doevents, or halt the execution of that function with sleep)
    3. continue with app
    Last edited by VaxoP; Oct 27th, 2006 at 02:29 AM.

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