Statement similar to VB's "DoEvents"

This is a discussion on Statement similar to VB's "DoEvents" within the C++ Programming forums, part of the General Programming Boards category; In VB , it helped to put the word DoEvents somewhere in the body of a loop so the program ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    31

    Statement similar to VB's "DoEvents"

    In VB, it helped to put the word DoEvents somewhere in the body of a loop so the program would respond to a user while the loop was running...

    For instance (VB Code samples):

    Dim lCount As Long

    Example 1:

    Do While lCount < 10000000
    lCount = lCount + 1
    Loop

    Example 2:

    Do While lCount < 10000000
    lCount = lCount + 1
    DoEvents
    Loop
    The first loop above would not respond to a user clicking a button or minimizing the form until the loop was finished, appearing as if the application was frozen. Also, it can take away from interaction with other apps because it usually takes 99% of your CPU time...

    The second allows interactivity while the loop is counting (so the form can be minimized, etc.). It basically allows users to still do stuff in Windows while the loop counts mindlessly away in the background.


    Is there anything similar in C++ to avoid these "frozen" type symptoms?
    Welcome to the funhouse, where strange mirrors reflect the faces of insanity.

  2. #2
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    so u want the loop to be peformed when you click the mouse button? and the number gets smaller or wutever each click
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    31
    Not exactly.

    What I'm looking for is a statement that allows the user to do stuff while the loop is running...

    The steps would look like this:

    1. Start the loop
    2. Loop counts....
    3. User does stuff

    Here's a better example:


    Dim bExitLoop As Boolean
    Dim lCount As Long

    Private Sub Command1_Click()

    bExitLoop = True

    End Sub

    Private Sub Form_Load()

    bExitLoop = False

    Do While bExitLoop <> True

    lCount = lCount + 1
    DoEvents

    Loop

    End Sub
    What happens here is as soon as the form is visible to the user (Form_Load()), the loop begins counting. It won't stop until the user clicks the command button and sets the boolean flag to True.

    Without the DoEvents statement, the command button's click event won't register with the program because the loop is taking up too much of the CPU time - it gives the impression that the program has "frozen". Without the DoEvents statement, it also decreases interactivity with other applications.

    For instance, if the user pressed ALT + TAB to switch to PhotoShop while the loop was counting, without the DoEvents statement, PhotoShop may not actually come up... and if it did and you minimized it, the VB application's form would have a "ghost" image of PhotoShop on it (or may just appear white) because Windows cannot re-draw the form.

    The DoEvents statement allows windows to process other messages while the loop is running, thus allowing the user to click the button and actually have it register, minimize the form, use other applications and switch back to the VB app...
    Last edited by XenoCodex Admin; 07-25-2002 at 04:01 PM.
    Welcome to the funhouse, where strange mirrors reflect the faces of insanity.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Do you know about a thing called Message Loop? Well, i believe that DoEvents does something to the messageloop so the user can respond. I dont know much VB so I cant say that for sure. But if you create a window using CreateWindowEx and keep checking for messages in the window loop you should be fine i guess. Its the same if you work on MFC or WTL application too.
    I hope that helped.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    One way would be to spawn a thread which would do all of your intense looping, and let another thread handle the GUI.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    31
    I like the thread idea...

    I'd never heard of Message Loop - I'm relatively new to C++ and find I'm still "thinking in VB" sometimes...
    Welcome to the funhouse, where strange mirrors reflect the faces of insanity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  2. Using while loop for if statement.
    By Galens in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2008, 03:14 PM
  3. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  4. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 02:46 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21