Results 1 to 8 of 8

Thread: Timer event

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Timer event

    Hello everybody!

    I have timer control on my form, and tick is 1000, which means that timer every second do code from it's tick event.

    I'm interesting how to do something when minutes changing.
    For example, now is 8:43, and I want program every second check is now 8:44, and when is 8:44 program have to do code from timers tick event.
    8:43 is not constant, 8:43 is variable.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Timer event

    Does it have to be on the sytem time minute, or do you just want to run some additional code every 60 seconds. If it is the latter, just add a counter variable and when it reaches 60, clear it, and do whatever you want. If it is based on the system time, then I think the easiest solution would be to save Date.Now.Minutes to a variable, and on every timer tick, check to see whether Date.Now.Minutes is the same as what you saved.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: Timer event

    I solved my problem!

    Tick of my timer is 1000 (so check every second)

    Dim second as Integer
    second = Now.Second

    If second = 0 then
    'minutes changed
    Else
    End If
    End Sub

    That's it, and timer doesn't ballast processor.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Timer event

    There is a slight problem with that. Because the Tick event is raised on the UI thread, it may get queued and you may have more than a second between Ticks. It's a slight risk but you may actually go straight from 59 to 01, missing the minute change.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Timer event

    Timers shouldn't be used to time things for the reasons JMC pointed out. Use a StopWatch for that. In your case use a combination of both.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Timer event

    I'd say just check the minute and watch for that to change.
    My usual boring signature: Nothing

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Timer event

    This method has served me well.

    Code:
        Dim stpw As New Stopwatch
        Dim IntervalWatchingFor As TimeSpan = New TimeSpan(0, 0, 1)
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = CInt(IntervalWatchingFor.TotalMilliseconds / 2)
            stpw.Reset()
            stpw.Start()
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) Handles Timer1.Tick
            'if the stopwatch is >= IntervalWatchingFor then...
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Timer event

    Why use a StopWatch? Just checking dates uses much less code, and is less complicated:
    vb.net Code:
    1. Private LastMinute As Date = Date.Now
    2.     Private Sub Timer1_Tick(ByVal sender As System.Object, _
    3.                             ByVal e As System.EventArgs) Handles Timer1.Tick
    4.         If Date.Now.Subtract(LastMinute).TotalMinutes >= 1 Then
    5.             LastMinute = Date.Now
    6.             ' Do whatever
    7.         End If
    8.  
    9.     End Sub

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