Results 1 to 9 of 9

Thread: New Day Event?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Question New Day Event?

    I need to create a procedure that will run every time a new day starts (at 00:00) if the program is running at that time.

    Is there some kind of event for the date changing?

    The only other way I can think of doing it is to have a timer running all the time to check if the date has changed or not.

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

    Re: New Day Event?

    There's nothing special about a new day, as far as I know, so running the timer would be the best way to go. The question you'd have to ask is how precise you need the timing to be. If you can flex a few minutes, then set the interval to a few minutes and just run it. If you need it to be pretty precise, then set the take the current time and set the interval such that it will tick within a minute or two prior to the end of the day. In that tick, set the interval to 1000 (or even less if you want to be more precise), and check the time each tick until the desired time is hit, run your actions, then set the tick interval back to the time needed to get within a minute or two of midnight, again.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Re: New Day Event?

    Ah yes that's a good idea, I hadn't thought about changing the interval
    It doesn't have to be too precise, I'm just making a program that will record how many key presses etc I make each day.
    Thanks a lot!

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: New Day Event?

    I need to create a procedure that will run every time a new day starts (at 00:00) if the program is running at that time.
    And what happens if it's not?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Re: New Day Event?

    Quote Originally Posted by dunfiddlin View Post
    And what happens if it's not?
    Well nothing will go wrong as such, it's just that my statistics won't be as accurate if it doesn't happen at the same time every day

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: New Day Event?

    Well that's kinda what I figured so I was wondering whether a better solution would not be to use the Windows Task Scheduler to interact with your program rather than trying to rig up some kind of new day event within it.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Re: New Day Event?

    The way I have set the program up is that the number of times each key has been pressed that day is in an array and the array only gets added to the database either when the program closes, the computer shuts down or hopefully, when a new day starts. When the new day starts the array values need to be set back to 0 so that it starts counting again.

    How would I use Windows Task Scheduler to help me with this? I have never used it before.

    The only way I can think of is by shutting the program down at 11:59 and starting it again at 00:00 as shutting it down will save that day's data to the database and then starting it again will set everything to 0 for the new day

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

    Re: New Day Event?

    Something like this I think

    Code:
        Dim aTimer As System.Timers.Timer
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            aTimer = New System.Timers.Timer()
            AddHandler aTimer.Elapsed, AddressOf Timer_Tick
            aTimer.AutoReset = True
            aTimer.Interval = untilEndOfDay()
            aTimer.Start()
        End Sub
    
        Dim saved As Boolean = False
    
        Private Sub Timer_Tick(sender As Object, e As EventArgs)
            'this event is NOT raised on the UI thread
            If Not saved Then
                aTimer.Interval = 60 * 1000 'one minute
                saved = True
                '
                'do the save here
                '
            Else
                aTimer.Interval = untilEndOfDay()
                saved = False
            End If
        End Sub
    
        Private Function untilEndOfDay() As Double
            Dim d As DateTime = DateTime.Now
            'for testing
            'Dim endOfDay As DateTime = DateTime.Now.AddSeconds(10)
    
            Dim endOfDay As DateTime = New DateTime(d.Year, d.Month, d.Day, 23, 59, 59, 999)
            Dim ts As TimeSpan = endOfDay - d
            Return ts.TotalMilliseconds
        End Function
    edit:

    An alternative load event handler that special cases the program starting at 00:00:00

    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            aTimer = New System.Timers.Timer()
            AddHandler aTimer.Elapsed, AddressOf Timer_Tick
            aTimer.AutoReset = True
            Dim d As DateTime = DateTime.Now
            If d.Hour = 0 AndAlso d.Minute = 0 AndAlso d.Second = 0 Then
                aTimer.Interval = 1
            Else
                aTimer.Interval = untilEndOfDay()
            End If
            aTimer.Start()
        End Sub
    Last edited by dbasnett; May 12th, 2013 at 09:04 AM.
    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Re: New Day Event?

    This seems like a good solution, I will try and implement it sometime this week, thanks!

Tags for this Thread

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