【发布时间】:2020-10-15 16:49:43
【问题描述】:
PowerModeChanged 事件似乎有问题(关于它的许多问题),但我找到的答案都没有帮助我。
我有一个运行 .NET Core 3.1 的 C#/WPF Windows 客户端应用程序(不是服务,没有什么花哨的,只是一个普通的 GUI 桌面应用程序)。我有一些事情发生在计时器上,我需要知道我什么时候错过了一个或两个计时器,因为笔记本电脑进入了睡眠状态(或暂停或其他任何事情)。
这是我的主窗口中的内容,我正在抓取我能找到的每一个事件:
private void OnLoaded(object sender, RoutedEventArgs e)
{
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += (a, b) => LiveTimer();
timer.Interval = new TimeSpan(0, 0, 10);
timer.Start();
SystemEvents.PowerModeChanged += OnPowerModeChanged;
SystemEvents.SessionSwitch += OnSessionSwitch;
}
protected override void OnDeactivated(EventArgs e)
{
Debug.WriteLine("MainWindow.OnDeactivated()");
Log.Info(this, "Main window deactivated");
base.OnDeactivated(e);
}
protected override void OnStateChanged(EventArgs e)
{
Debug.WriteLine("MainWindow.OnStateChanged()");
Log.Info(this, "Main window state changed [WindowState=" + WindowState + "]");
base.OnStateChanged(e);
}
private void OnSessionSwitch(object sender, SessionSwitchEventArgs e)
{
Debug.WriteLine("MainWindow.OnSessionSwitch(sender=" + sender + ", e [Reason=" + e.Reason + "])");
Log.Info(this, "Session switched because " + e.Reason);
}
private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
Debug.WriteLine("MainWindow.OnPowerModeChanged(sender=" + sender + ", e [Mode=" + e.Mode + "])");
Log.Info(this, "Power mode changed to " + e.Mode);
}
private void LiveTimer()
{
Debug.WriteLine("MainWindow.LiveTimer() at " + DateTime.Now.ToString("HH:mm:ss.fff"));
}
因此,如果我在笔记本电脑上运行此应用程序并等待屏幕锁定(5 分钟)并等待笔记本电脑进入睡眠状态(10 分钟),我会在调试控制台中看到以下内容:
MainWindow.LiveTimer() at 10:07:17.711
(this happens every 10 seconds)
MainWindow.LiveTimer() at 10:12:20.593
MainWindow.OnSessionSwitch(sender=Microsoft.Win32.SystemEvents, e [Reason=SessionLock])
MainWindow.LiveTimer() at 10:12:30.614
(this happens every 10 seconds)
MainWindow.LiveTimer() at 10:17:24.173
(and now it goes to sleep)
(and then I wake it up)
MainWindow.LiveTimer() at 10:31:28.519
MainWindow.OnSessionSwitch(sender=Microsoft.Win32.SystemEvents, e [Reason=SessionUnlock])
MainWindow.LiveTimer() at 10:31:38.716
(and now we're back to firing every 10 seconds)
基本上,我绝对不会收到关于笔记本电脑要进入睡眠状态的任何通知,而且我暂时不会收到计时器消息。
我的应用如何发现它已经进入睡眠状态并被重新唤醒?
【问题讨论】:
标签: c# wpf .net-core sleep-mode