CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: DoEvents

  1. #1
    Join Date
    Mar 2002
    Posts
    13

    DoEvents

    would someone educate me about DoEvents ?

    I am using DoEvents in the following function under the impression that When it waits for the time to elapse , let the OS do something else.
    But my Supervisor has told me NEVER user DoEvents as it will help up resources.
    I thought DoEvents is to release the resources.

    could you please advise me on this ?

    Code:
    Public Sub Delay(NumberOfSeconds As Integer)
        Dim start As Single
        Dim Kounter As Integer
        On Error Resume Next
        start = Timer
        Kounter = 0
        
        Do While Timer < start + NumberOfSeconds
            DoEvents
        Loop
        
    End Sub

  2. #2
    Join Date
    Apr 2002
    Location
    Cleveland, Ohio, USA
    Posts
    200
    DoEvents yields execution so that the operating system can process other events. In other words, it prevents your program or function from locking up the system resources. It is necessary if you want to have a button allowing the user to cancel a process.

    DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent.

    This doesn't cause the current application to give up the focus, but it does enable background events to be processed.

    There are dangers however. Any time you temporarily yield the processor within an event procedure, make sure theprocedure is not executed again from a different part of your code before the first call returns; this could cause unpredictable results. In addition, do not use DoEvents if other applications could possibly interact with your procedure in unforeseen ways during the time you have yielded control.

    The VB help files have more information. In general, it is not something to be avoided, but rather should be used to prevent your program from using up all the system's resources.

  3. #3
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    Code:
        Do While Timer < start + NumberOfSeconds
            DoEvents
        Loop
    In that loop, the DoEvents will be called, then the form will process events (if any) like mouse move and button click, but after some milliseconds, the loop will continue. The Loop will return to the DO and execute the condition (Timer < start + NumberOfSeconds), those last two operation take ressources. Since the loop might be executed many times per seconds, you'll end with a cpu usage 100% maybe. That's because the processor do not end any operations for enough time. When it is not executing the loop, it is processing the form events.

    To arrange that, you may use a TIMER control, or, in you Do Loop, you could use a Sleep() , for example

    Code:
        Do While Timer < start + NumberOfSeconds
    	Sleep(1000) 'Sleep API, make the application sleep for 1 second
            DoEvents
        Loop
    Using a sleep with help the processor ressource, but events will only be processed once per seconds

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  4. #4
    Join Date
    Oct 2002
    Location
    India
    Posts
    103
    I think u must supervise ur supervisor,

    DoEvents helps to control the CPU USAGE by ur application

    DoEvents is usefull so that ur application does not lock up resources
    Last edited by Nilesh_S_Gokhal; November 2nd, 2002 at 03:33 AM.
    Nilesh
    Pune, Maharashtra
    India

  5. #5
    Join Date
    Aug 1999
    Posts
    28
    I use the DoEvents when I want to show a progressbar (thats on a seperate form) for any long actions I am doing. If I do not add the DoEvents command after each reference to the progressbar, the progressbar will not appear to be doing anything.

    Example:

    frmProgress.pgbProgress.Max = (Value)
    frmProgress.Show
    DoEvents
    (I have to add the DoEvents to make the frmProgress to appear)

    Long data manipulation event occuring ................
    frmProgress.pgbActionProgress.Value = (value)
    DoEvents
    (I have to add the DoEvents so the user can see the pgbProgress bar advancing)

    This is just a little example of the DoEvents keyword.
    Hope it helps you understand it a little better.

  6. #6
    Join Date
    Oct 2002
    Posts
    4
    DoEvents solved a problem for me a week ago.
    I am running a simulation program in a very havy 3D loop
    for z = 0 to ..
    for y = 0 to ..
    for x = 0 to ...
    ...
    ... LOT of calculations
    ...
    next
    next
    next

    The problem with this code is that I could not interact with my application window. The program did not handle/process any events. TOO BUSY.

    I modified the code as follows:
    for z = 0 to ..
    for y = 0 to ..
    for x = 0 to ...
    DoEvents
    ...
    ... LOT of calculations
    ...
    next
    next
    next

    Now the program listens to any control event I, as a user, generate. Before, the application was frozen completely.

    When a heavy loop in your code prevents other events to be processed, the DoEvent is very usefull (when you put it at the right place - as deep as possible.

  7. #7
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    DoEvents is usefull since Threading in VB is not well supported. But remember that your loop MUST END. If you use a loop with DoEvents to show time on a label of your form, you'll need the loop to always work to show time each seconds. This will lock ressources and cpu usage, so not a gooc choice.

    Use DoEvents when you do any hard loop (to make sure the program do not freeze or to show echo on your form) and when that loop finish. For example, copying a lot of files and showing the progress on a progress bar. The ressources and cpu usage will be at top usage, but this will eventually end.

    If you need something that will always be "looping", use a timer control

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

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

Featured