0

i need a little help with a timer funtion.

I have await Times in Minutes (3m, 5m, 15m, 30m, 60m, 120m........ 1440m and so on) that must fire on the exact time a function and must ignore the starttime...

Also like so:

    Starttime: 00:51 > 3m > Starts at 00:54
    Starttime: 00:51 > 5m > Starts at 00:55
    Starttime: 00:51 > 30m > Starts at 01:00
    Starttime: 09:51 > 120m > Starts at 10:00
    Starttime: 11:51 > 240m > Starts at 12:00
    Starttime: 11:51 > 480m > Starts at 16:00
    Starttime: 21:51 > 1440m > Starts next Day at 00:00
    ... and so on

i have this code:

            int Minutes = value;
            TimeSpan calc =  TimeSpan.FromMinutes(Minutes - DateTime.Now.Minute % Minutes);
            double calc1 = calc.TotalMinutes * 60 * 1000;
            return Convert.ToInt32(calc1);
            ....
            await Task.Delay(returnvalue);

Seems not working with Minutes Values greater then 60 Minutes, why? anyone know a better working solution? thx <33

Shanapoia
  • 23
  • 4
  • What are you trying to achieve here? Is this a Task Scheduler like code - If So use Quartz.net instead. If you are trying to learn something else - then rephrase the question/problem with more clarity. – Prateek Shrivastava Mar 18 '21 at 00:05
  • @PrateekShrivastava I have a small Backtasting Tool, and inside in a function i must wait for the exact Time like my example with Starttime. – Shanapoia Mar 18 '21 at 00:09
  • So you are trying to say that your Console prints show wrong Starts at timing. ex: 09:51 + 120m == Says Starts at 10:00 -- where as it should Start at: 11:51 -- Can you show code for that area? – Prateek Shrivastava Mar 18 '21 at 00:16
  • Causing something to occur with precise timing is hard in an OS that doesn't care that much and may interrupt what you are doing at any time. – Jeremy Lakeman Mar 18 '21 at 00:21
  • You say "and so on" as if it makes sense. Your example makes no sense. If "5m" starts at "00:55" and "30m" starts at "01:00", then I have no clue what algorithm you are using for your time arithmetic. – Tim Roberts Mar 18 '21 at 00:26
  • 1
    Your question is not entirely clear. However, based on the examples you gave, it appears that you want the timer to expire on every even multiple of the interval you provide, relative to the start of a day (i.e. midnight). If so, see duplicate. Do be aware that platforms where .NET is supported are not real-time operating systems, so you will never be able to get your method to be called _exactly_ at the due time. But it would typically be within a few dozen milliseconds at worst. – Peter Duniho Mar 18 '21 at 00:29
  • Extra credit to @PeterDuniho for that insight. I didn't spot that, but now it's obvious. He's building `cron`. – Tim Roberts Mar 18 '21 at 00:35

1 Answers1

0

Use the clock and a loop.

var triggerTime = DateTime.Now.AddMinutes(minutes);

while (DateTime.Now < triggerTime)
{
    await Task.Delay(1000);
}

MessageBox.Show("Time's up!");
John Wu
  • 50,556
  • 8
  • 44
  • 80