0

I have a timer that is going to trigger every 15 seconds.

But I want it to start at a round time: 14:00 ; 14:15 ... 16:45 for examples.

So I don't want it to start when the form loads for example, as it's going to be at a random time...

How could I do that ? I was thinking about a timer and so check every second if I've got Now.Seconds = 0 AND Now.Minutes = 0,15,30 or 45. But it looks like a very ugly and cpu consuming solution for almost nothing...

Any advice ? :)

Nota : Framework 4.0 for a WinForm application. vb.net language.

DanLo'
  • 13
  • 6
  • 3
    set a timeout. you know when you want it to start, you know what time it is NOW, so calculate `start time - now` and you'll get how long you have to delay execution. – Marc B Aug 22 '16 at 16:34
  • 1
    Your computer won't even notice the load from checking the time every second. – Bradley Uffner Aug 22 '16 at 16:38
  • 1
    `... and cpu consuming` Did you measure that? – LarsTech Aug 22 '16 at 16:38
  • 1
    is there any reason you are unable to just launch your program with task scheduler? – soohoonigan Aug 22 '16 at 17:37
  • I wouldn't worry about `ugly` - if it works it works (and it should be maintainable as well when you come back to it a year from now.) Post the code if you want more. – rheitzman Aug 22 '16 at 18:05
  • If you don;t want to do something every 15 seconds then why have a `Timer` that `Ticks` every 15 seconds? If you want a `Tick` at a specific time then set the `Interval` such that the `Tick` will occur at a specific time. If you want to change the interval after the first `Tick` then do exactly that, i.e. set the `Interval` in the `Tick` event handler. – jmcilhinney Aug 23 '16 at 03:01
  • I wanted to know if a smarter solution exists, as it looks like a pain to manipulate time programming-ly with timers... but if no, okay. ^^ (when you see how not-easy it is to create a control that displays real time..!) Could have been cool to have embedded Event Handlers linked to Time/Date. :) – DanLo' Aug 23 '16 at 08:35
  • @jmcilhinney, as it is not a specific time, but the closest time that follows my conditions above, how could I calculate that ? – DanLo' Aug 23 '16 at 15:44
  • So many times people seem to think that programming exists in a vacuum. It doesn't. What if you had to do this process manually for yourself? Would you just throw your hands up in the air and say you couldn't do it? I doubt it. In fact, I think you'd find it fairly easy. Think about the actual steps you would perform in that case. Put those steps in into clear words, then write code to implement those words. Don't just try to pluck code out of thin air. Code is NOT the solution. Code is an implementation of the solution. Solve the problem first, then implement that solution. – jmcilhinney Aug 23 '16 at 23:47
  • So, what would you do? You'd look at the current time and then look at the list of times you want the task to be performed and determine which of those list times is the earliest but also later than the current time. That really should be obvious to anyone and requires no programming experience whatsoever. Now the programming part is to write code to do that but it's a lot easier to write code when you know what the code is actually supposed to do, yes? – jmcilhinney Aug 23 '16 at 23:49
  • Thanks for your comments jmcilhinney. Heck, I know it's is not hard to implement ! Sorry if you though I'm looking for in the vacuum. ;) The fact is that the way of doing it (when I put the steps in clear words ;) ) seems quite "heavy" : it's a lot of tests to find this "delta time" while I could just check every second. So my question is more like a "don't you know some useful time-related functions that could help me". Looks like the answer is no. :) – DanLo' Aug 24 '16 at 19:28

1 Answers1

0

This isn't exactly what you're asking for, but I think you could modify it to work for you:

Need A Timer To Fire At Specific Time And Every 5 Minutes Until Job Complete - StackOverflow

Community
  • 1
  • 1
Michael Armes
  • 1,056
  • 2
  • 17
  • 31