Results 1 to 8 of 8

Thread: [2005] Most efficient way to test for specific dates and times

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Question [2005] Most efficient way to test for specific dates and times

    What's the most efficeint way to test for a specific date and time and, perhaps, pop up something at the exact time?

    I thought about using a System.Windows.Forms.Timer, making the tick count equal to about 24 hours until it gets close to the time specified, then change the tick to something else like 8 hours and keep bringing that down but many have said the System.Windows.Forms.Timer is inefficient and inaccurate and this has to be accurate down to the second.

    Any recommendations? I thought about making checking the system's time every second but that would be very inefficient. Perhaps checking the system's time every so many hours until it gets closer to the time, then start every 5 seconds to 1 second or something?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  2. #2
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] Most efficient way to test for specific dates and times

    You can calculate the interval you need to set the timer for, and just have it tick once instead of ticking every second or so and having to check if its the time you want to run it...
    VB Code:
    1. 'timespan representing the next full hour past the current time
    2.         Dim TargetTime As New TimeSpan(Date.Now.Hour + 1, 0, 0)
    3.         'timespan of the current time
    4.         Dim CurrentTime As New TimeSpan(Date.Now.Hour, Date.Now.Minute, Date.Now.Second)
    5.         'subtracts current time from target time, displays result (time until the next hour)
    6.         Dim ResultTime As TimeSpan = TargetTime.Subtract(CurrentTime)
    7.         MessageBox.Show(ResultTime.Hours & ":" & ResultTime.Minutes & ":" & ResultTime.Seconds)
    That is just an example of getting the time until the next full hour, easily modified to be a particular hour of the day. You can use the .TotalSeconds method, or something, of the result timespan in order to set your interval of your timer. That way, the timer ticks only one time, at the time you wish to run the code...

  3. #3

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] Most efficient way to test for specific dates and times

    That was one of my ideas but what if the timer becomes off when it's getting close? I may have issues where the reminder/popup/whatever is late.

    Would it be better to use one of the other Timer classes included in the .Net 2.0 framework?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  4. #4
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] Most efficient way to test for specific dates and times

    well the Timer interval is what you would set once you calculate how long it is until that time, as was stated in the above post. The timer would only have to tick one time, and that one time is at the time that you want, instead of having a timer constantly ticking and checking the time, etc. hence "more efficient". Is the Timer clases in 2.0 any different than in 1.1?

  5. #5

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] Most efficient way to test for specific dates and times

    Quote Originally Posted by gigemboy
    Is the Timer clases in 2.0 any different than in 1.1?
    Honestly, I'm not too sure which is why I posted this question. There is System.Windows.Forms.Timer, System.Threading.Timer, and System.Timers.Timer.

    I can't find the MSDN write-up on each. I really need to start book marking stuff.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  6. #6
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] Most efficient way to test for specific dates and times

    well the Forms.Timer is for of course windows forms. If you are coding a service, then you would use System.Timers.Timer I think, and System.Threading.Timer I believe is a timer used that is safe for threading (sorry for not being definite but its late and Im sleepy hehe)...
    Last edited by gigemboy; Apr 16th, 2006 at 04:37 AM.

  7. #7

  8. #8

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] Most efficient way to test for specific dates and times

    There it is! Perfect, thanks
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

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