Results 1 to 4 of 4

Thread: DoEvents doesn't always work

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    195

    DoEvents doesn't always work

    Hello everybody. I used the following code to try the DoEvents function. On a form put Timer1 with Interval=1000 and Timer2 with Interval=5000:

    Code:
    Dim t As Integer
    Private Sub Timer1_Timer()
    t=t+1
    End Sub
    Private Sub Timer2_Timer()
    Static i as Integer
    i=i+1
    Debug.Print i
    t=0
    Do Until t=10
    DoEvents
    Loop
    i=i-1
    End Sub
    If you launch the code, you'll see that i always equals 1. But it shouldn't be so, because of the DoEvents, which should allow the Timer2_Timer event to fire while another Timer2_Timer() Sub is being executed. Do you know why this is so? Thanks.

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

    Re: DoEvents doesn't always work

    You have a logic error here: Timer2_Timer event is never fired more than once, there will never be two Timer2 events running at the same time. Thus as you enter the sub you set i to 1 and then when you exit it is set back to 0.

    Also, I don't think this kind of coding would give any real benefit (but I'm generally not a fan of DoEvents anyway).

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    195

    Re: DoEvents doesn't always work

    Quote Originally Posted by Merri View Post
    You have a logic error here: Timer2_Timer event is never fired more than once, there will never be two Timer2 events running at the same time. Thus as you enter the sub you set i to 1 and then when you exit it is set back to 0.

    Also, I don't think this kind of coding would give any real benefit (but I'm generally not a fan of DoEvents anyway).
    This has to be a by-design. They designed the Timer event in such a way that it can only fire after the preceding one's sub has been executed. Isn't it?

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: DoEvents doesn't always work

    I am 98% sure the Timer control is a repeating one-shot timer, not a simulated real-time clock interrupt. So what I'm saying is that I strongly suspect (but haven't proven) that the Timer is re-armed only when you exit its event handler.

    This may explain the wailing and gnashing of teeth resulting in people saying Timer controls are "not accurate." They may be confusing an apple with a banana.

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