C
C#2y ago
.dark4

✅ Schedule event at future DateTime based on DateOnly input, doesn't work if called <2min before

public static void ScheduleMethod(DateOnly inputDate)
{
DateTime triggerTime = new DateTime(inputDate.Year, inputDate.Month, inputDate.Day, 7, 0, 0).AddMonths(-6); // 7am 6months before input date

// Calculate the time interval between now and the trigger time
Console.WriteLine("DateTime Now: " + DateTime.Now);
Console.WriteLine("Trigger Time: " + triggerTime);
Console.WriteLine("Time until trigger: " + (triggerTime - DateTime.Now));

var delay = (int)(triggerTime - DateTime.Now).TotalMilliseconds;
if (delay > 0)
{
timer = new Timer(state => Console.WriteLine("event!!")), null, delay, Timeout.Infinite);
}
}
public static void ScheduleMethod(DateOnly inputDate)
{
DateTime triggerTime = new DateTime(inputDate.Year, inputDate.Month, inputDate.Day, 7, 0, 0).AddMonths(-6); // 7am 6months before input date

// Calculate the time interval between now and the trigger time
Console.WriteLine("DateTime Now: " + DateTime.Now);
Console.WriteLine("Trigger Time: " + triggerTime);
Console.WriteLine("Time until trigger: " + (triggerTime - DateTime.Now));

var delay = (int)(triggerTime - DateTime.Now).TotalMilliseconds;
if (delay > 0)
{
timer = new Timer(state => Console.WriteLine("event!!")), null, delay, Timeout.Infinite);
}
}
It seems to work fine during testing, but when I call ScheduleMethod(DateOnly); too close to the trigger time I get an error. System.ArgumentOutOfRangeExemption The added or subtracted value results in an un-representable DateTime. I tested this morning at approx 6:53am and it calculated the Time unit trigger, closed and tried again at approx 6:58am and got the error.
2 Replies
Axiss
Axiss2y ago
I'm not sure why you are getting an ArgumentOutOfRange exception, you might want to log your inputs so you can create a debuggable test case.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server