❔ Protips to write thing like: raise Alarm when Time hits DateTime.Now

How can I achieve this? I know that I need to use events. DispatcherTimer as well?
2 Replies
DaVinki
DaVinki2y ago
Why would you want to wait until DateTime.Now? But if you want to wait until a certain time, you should look into DateTime and TimeSpan Easy to work with. DateTime is an instance of time and TimeSpan is a span of time, say a few seconds, hours or days. Task.Delay takes a TimeSpan and subtracting two DateTime objects returns a TimeSpan between the two Is this an example of what you want?
public static async Task Main()
{
await WaitUntilNextMinuteStarts();
Console.WriteLine("It is now the next minute");
}

public static async Task WaitUntilNextMinuteStarts()
{
var now = DateTime.Now;
var goalTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute + 1, 0);
await Task.Delay(goalTime - now);
}
public static async Task Main()
{
await WaitUntilNextMinuteStarts();
Console.WriteLine("It is now the next minute");
}

public static async Task WaitUntilNextMinuteStarts()
{
var now = DateTime.Now;
var goalTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute + 1, 0);
await Task.Delay(goalTime - now);
}
A Timer would repeat itself unless you set the period to an infinite time span
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