Results 1 to 4 of 4

Thread: [RESOLVED] a tale of two timers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Resolved [RESOLVED] a tale of two timers

    Hi folks.

    I have a form with a label. I want the form to display a message every 5 seconds, and the message to display for 700ms before closing. So I used timer1 with an interval of 5000ms, and timer2 with an interval of 700ms. But the message seems to be displaying every 5 seconds(what I want), but is displayed for 5 seconds(what I don't want). I want the message to display only for 700ms. Where is the problem in the code? Thanks in advance.

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer2.Start()
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            If Me.Visible = True Then
                Me.Visible = False
            Else
                Me.Visible = True
                Me.Label1.Text = "Test Message"
            End If
            Timer2.Stop()
        End Sub
    End Class

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: a tale of two timers

    Try using a single timer running often enough to do both jobs of turning on and turning off the message.

    Retain the last time turned on so you can manage the on/off.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: a tale of two timers

    Thanks for your reply. I will give your suggestion a go, but before that, could you explain why my program displays the message for 5 seconds, instead of 7/10 of a second? I am looking at my code, and I just can't figure out this strange behaviour. Thank you.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: a tale of two timers

    It is because you have explicitly tied the speed to Timer1.

    Each time Timer1 fires, you start Timer2. When Timer2 fires, you do the work and disable Timer2 - and then have to wait until Timer1 starts it again.

    The simplest solution is to make two changes (add a line, and move one):
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer2.Start()
            Timer1.Stop() 
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            If Me.Visible = True Then
                Me.Visible = False
                Timer2.Stop()
            Else
                Me.Visible = True
                Me.Label1.Text = "Test Message"
            End If
        End Sub
    End Class
    szlamany's suggestion is more elegant and self-contained, but takes a bit more code... this is one way it could be done (with just Timer1):
    Code:
    Public Class Form1
    
        Private TimerStartedAt as Date
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            TimerStartedAt = Now() 
            Timer1.Interval = 100
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Select Case (Now - TimerStartedAt).TotalMilliseconds
            Case 5000 To 5700
                If Not Me.Visible Then
                    Me.Visible = True
                    Me.Label1.Text = "Test Message"
                End If
            Case > 5700
                Me.Visible = False
                Timer1.Stop()
            End Select
        End Sub
    End Class
    Last edited by si_the_geek; May 5th, 2013 at 02:33 AM.

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