Results 1 to 8 of 8

Thread: DoEvents

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    87

    DoEvents

    what extactly does a DoEvents do? does it pause the program so it wont lag?

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    It pauses the execution at the DoEvents clause and processes other code on the stack. While 'moving' code after the DoEvents to the top of the stack(if you imagine it as first in first out, FIFO, so code after it would be 'last in, last out').

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    In other words it gives running back to windoze and lets windoze update the forms etc. instead of hanging the app (ctrl+alt+del -> status "not responding").

    So if you do processor intensive programming loop, put an update on the screen for the user and use doevent to update it/keep your app open.

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787
    your computer is your friend and not to include doevents is like not letting it breathe,

    doevents lets your computer breathe

  5. #5
    Frenzied Member Robbo's Avatar
    Join Date
    Jan 2001
    Location
    Bradford
    Posts
    1,143
    yeah its kinda used when ur form is doing a big task, which will take a few seconds to do, so it doesnt "hang" and look like its crashed
    -----------------------------------------------
    "The hall is rented,"
    "the orchestra is engaged,"
    "its now time to see if you can dance!"
    Q, Q-Who, Star Trek The Next Generation
    -----------------------------------------------
    General Work day

    -----------------------------------------------
    DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    87
    so when i put doevents in a piece of code, does it pause that part? i dont really get it, can someone give me an example?

  7. #7
    Frenzied Member Robbo's Avatar
    Join Date
    Jan 2001
    Location
    Bradford
    Posts
    1,143
    ok for an example i have an application which runs a few SQL quiries and display in a grid, to show the results in the first part, while the next SQL command runs its best to put the do events in, otherwise you will probably have to wait till the two SQL commands have completed and are then displayed, doevents is more for what the form shows
    -----------------------------------------------
    "The hall is rented,"
    "the orchestra is engaged,"
    "its now time to see if you can dance!"
    Q, Q-Who, Star Trek The Next Generation
    -----------------------------------------------
    General Work day

    -----------------------------------------------
    DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle

  8. #8
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    DoEvents example:

    But a button a form, and put this code in the CLICK event of the button.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i As Long
    3. Dim i2 As Long
    4. Command1.Enabled = False
    5. For i = 1 To 50000
    6.     For i2 = 1 To 10000
    7.        
    8.     Next
    9. Next
    10.  
    11. MsgBox "Done"
    12. Command1.Enabled = True
    13.  
    14. End Sub

    Run the app, click on the button.
    Try and move the form - you can't until the loops have finished and you have dismissed the msgbox. This is because the PC is concentrately solely on the loops in the code and is not responding to other events (ie - you clicking and dragging the form with the mouse) until it has finished

    Now edit the code slightly to:

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i As Long
    3. Dim i2 As Long
    4. Command1.Enabled = False
    5. For i = 1 To 50000
    6.     [b]DoEvents[/b]
    7.     For i2 = 1 To 10000
    8.        
    9.     Next
    10. Next
    11.  
    12. MsgBox "Done"
    13. Command1.Enabled = True
    14.  
    15. End Sub

    Now when you click the button you will notice it takes longer to get to the msgbox - however this time you CAN drag the form around the screen because you have given Windows the opportunity to deal with any other events via the DoEvents command.

    So, in essence, if you have a processor-intensive loop going on consider putting a DoEvents in to stop the program appearing to be "not responding" and allow the user to interact with it in other ways while the loop is running.

    However - be warned - because other events (eg button clicks etc) WILL fire while the loop is running you have to make sure you deal with it appropiately - in my example I disabled the button while the loop was running to stop the user pressing it again.. just because they can.. until it had finished the loop.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

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