Results 1 to 15 of 15

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

  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.

  2. #2
    Addicted Member
    Join Date
    Jun 2006
    Location
    Egypt
    Posts
    162

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

    Use a Timer
    Mohammed Sayed - Egypt - Anesthetist



    =

    =

  3. #3
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

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

    Have a look at this post by Jacob Roman. There are a couple of alternatives.
    http://www.vbforums.com/showthread.php?t=315416
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

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

    DoEvents lets Windows handle it's message queue for your program's process. You use it to let your program's windows and controls paint and response.

    Sleep will halt the execution of the process for given amount of milliseconds.

    As such there is nothing wrong with either; if you want, you may use GetQueueStatus API or whatever it was named to optimize DoEvents calls so that DoEvents isn't called when there is no messages to process. Sleep will give time for other programs, thus eliminating the 100% processor usage. So you can very well use:

    VB Code:
    1. Do Until Condition: DoEvents: Sleep 1: Loop

    However, if this condition is not from an external program but from within your own program, you might want to consider another kind of coding approach.

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

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

    Quote Originally Posted by Merri
    DoEvents lets Windows handle it's message queue for your program's process. You use it to let your program's windows and controls paint and response.

    Sleep will halt the execution of the process for given amount of milliseconds.

    As such there is nothing wrong with either; if you want, you may use GetQueueStatus API or whatever it was named to optimize DoEvents calls so that DoEvents isn't called when there is no messages to process. Sleep will give time for other programs, thus eliminating the 100% processor usage. So you can very well use:

    VB Code:
    1. Do Until Condition: DoEvents: Sleep 1: Loop

    However, if this condition is not from an external program but from within your own program, you might want to consider another kind of coding approach.

    I was going to recommend GetInputState but I didn't know about the other API.

  6. #6

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

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

    the code that was linked is no good, it seriously slows down my machine.. i cant move my mouse or anything. so that didnt work

    honestly the best way i can think of doing this is a timer..
    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
    There HAS to be a more elegant way..
    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 26th, 2006 at 11:30 PM.

  7. #7
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

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

    Quote Originally Posted by DigiRev
    I was going to recommend GetInputState but I didn't know about the other API.
    Don't use GetInputState in this kind of situation. This API call only polls the "calling thread's message queue"--meaning your program--and not the main/OS-based message queue--which is what DoEvents handles.

    I've always preferred using SleepEx over Sleep because it is interruptible--meaning if something happens your program can get working again without having to wait for its timer to end. It also lets the OS do its thing in a simpler/more efficient manner because you are basically telling the OS "Stop my program and do whatever you want to do 'til I need the CPU again".

  8. #8
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

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

    Quote Originally Posted by VaxoP
    There HAS to be a more elegant way..
    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
    There is a way to do it, but you can't do it in the same thread. It sounds like you want a VB version of C's fork() which is async. processing, and when it is done, you continue with the parent program. The only way I think you're going to be able to do that is use an ActiveX EXE or a separate program/process to crunch on your data and raise an event to your main/parent program that it can proceed.

  9. #9
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

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

    Not sure if this is what you're after, but if you're not calling an external app then you may be able to use an "off-screen" textbox's Change event. It's a kludge but works. Something like this:
    VB Code:
    1. Option Explicit
    2.  
    3. Dim intChange As Integer 'Text change flag. Use boolean in a real app.
    4.  
    5. Private Sub Form_Click()
    6.    RunSomeFunctionInTheBackground
    7. End Sub
    8.  
    9. Private Function RunSomeFunctionInTheBackground() As String
    10. 'do something that takes 10 seconds
    11.    'Blah, blah
    12.    'etc, etc
    13.  
    14. 'Ok, time to exit
    15.    RunSomeFunctionInTheBackground = "My output"
    16.  
    17. 'Change Text1.
    18.    If intChange = 1 Then
    19.       Text1.Text = "Finished"
    20.       intChange = 2
    21.    Else
    22.       Text1.Text = "Over and out"
    23.       intChange = 1
    24.    End If
    25. End Function
    26.  
    27. Private Sub Form_Load()
    28.    intChange = 1
    29. End Sub
    30.  
    31. Private Sub Text1_Change()
    32.    MsgBox "All done"
    33. 'Carry on...
    34.    Continue_With_App
    35. End Sub
    36.  
    37. Public Sub Continue_With_App()
    38.    'Blah, blah
    39.    'etc, etc
    40. End Sub

  10. #10
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

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

    with the text box change....

    if you have
    VB Code:
    1. private sub Textbox_change()
    2.  
    3. for i=0 to 100
    4.  me.print i
    5. next i
    6.  
    7. end sub
    8.  
    9. private mysub()
    10.  
    11.  'do some stuff
    12. textbox.text="xox"
    13.  
    14. 'do some more stuff
    15. end sub

    is it that the more stuff happens whilst 1 - 100 is printed? if not then why not just call the sub at the end of the other function?

    sorry, just thinking about it, but what are you doin? - maybe a better way? :S ta
    Wayne

  11. #11
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

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

    True. My bad. VB is (normally) single threaded. Not quite sure what VaxoP is after.

    EDIT: Ah, now I do... (multi threading)

    Google for "multi thread visual basic". You'll get about 10,000,000 hits with topics such as Intro to Multi-Threading in VB
    Last edited by schoolbusdriver; Oct 27th, 2006 at 05:44 AM.

  12. #12
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

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

    Quote Originally Posted by schoolbusdriver
    True. My bad. VB is (normally) single threaded. Not quite sure what VaxoP is after.

    EDIT: Ah, now I do... (multi threading)

    Google for "multi thread visual basic". You'll get about 10,000,000 hits with topics such as Intro to Multi-Threading in VB
    COOL!
    cheers... really helpful for me too - never realised that adding a thread was so easy...
    Wayne

  13. #13
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

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

    Quote Originally Posted by wpearsall
    COOL!
    cheers... really helpful for me too - never realised that adding a thread was so easy...
    Then don't forget to rate me

  14. #14

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

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

    so if i were to run my background process in another thread using multithreading.. how can i pause the main app until the background execution is done?

  15. #15
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

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

    VaxoP,

    Just for the record... DoEvents doesn't use 100% of your CPU your loop does. You are blaming DoEvents for bad coding...


    DoEvents allows other processes to execute. Take DoEvents out of your loop and it will still take 100% of the CPU so what does DoEvents have to do with it?

    If coded correct you would have your code in an event since VB is event driven and in that way nothing would happen until the event triggered. Your code that you have eecuting could trigger the event itelf. In this way your code would alway be in a wait state until something triggered it to do something else.

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