SafeDispatch/SafeMobileLIB_DLL/MidnightNotifier.cs
2024-02-22 18:43:59 +02:00

54 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Microsoft.Win32;
namespace SafeMobileLib
{
public static class MidnightNotifier
{
private static readonly Timer timer;
static MidnightNotifier()
{
timer = new Timer(GetSleepTime());
timer.Elapsed += (s, e) =>
{
OnDayChanged();
timer.Interval = GetSleepTime();
};
timer.Start();
SystemEvents.TimeChanged += OnSystemTimeChanged;
}
private static double GetSleepTime()
{
var midnightTonight = DateTime.Today.AddDays(1);
var differenceInMilliseconds = (midnightTonight - DateTime.Now).TotalMilliseconds;
return differenceInMilliseconds;
}
private static void OnDayChanged()
{
var handler = ItsMidnight;
if (handler != null)
handler(null, null);
}
private static void OnSystemTimeChanged(object sender, EventArgs e)
{
var handler = UserChangedDate;
if (handler != null)
handler(null, null);
timer.Interval = GetSleepTime();
}
public static event EventHandler<EventArgs> UserChangedDate;
public static event EventHandler<EventArgs> ItsMidnight;
}
}