如何检测当Windows 8进入睡眠状态或恢复 [英] How to detect when Windows 8 goes to sleep or resumes

查看:258
本文介绍了如何检测当Windows 8进入睡眠状态或恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有保持到服务器活着连接的应用程序,但是,如果用户走开和平板电脑进入睡眠状态,我想优雅地处理断开了,我也想重新登录时,用户唤醒平板电脑。



我试过在我把下面的代码在我的连接类,但他们从来没有被解雇。

  Application.Current.Suspending + = this.OnAppSuspending; 
Application.Current.Resuming + = this.OnAppResuming ;;


解决方案

有关桌面应用程序,你可以使用的 SystemEvents.PowerModeChanged 事件知道Windows是否会进入睡眠状态。我不知道,如果这个工程的片太多,但你可以尝试...



从MSDN:



•恢复操作系统即将从挂起状态恢复。



•StatusChange功率模式状态通知事件已经由操作系统提高。这可能表明弱或充电电池,AC电源和电池,或者在系统电源状态的另一个变化之间的过渡。



•挂起的操作系统关于暂停

  SystemEvents.PowerModeChanged + = OnPowerChange; 

私人无效OnPowerChange(对象S,PowerModeChangedEventArgs E)
{
开关(e.Mode)
{
情况下PowerModes.Resume:
断点;
情况下PowerModes.Suspend:
中断;
}
}


I have an app that keeps a connection alive to a server, however if the user walks away and the tablet goes to sleep, I would like to handle the disconnect gracefully, and I would also like to log back in when the user wakes the tablet.

I've tried in my putting the following code in my connection class, but they never get fired.

Application.Current.Suspending += this.OnAppSuspending;
Application.Current.Resuming += this.OnAppResuming;;

解决方案

For desktop apps, you can use SystemEvents.PowerModeChanged event to know if Windows is going into sleep state. I don't know if this works for the tablets too, but you can try it...

From MSDN:

• Resume The operating system is about to resume from a suspended state.

• StatusChange A power mode status notification event has been raised by the operating system. This might indicate a weak or charging battery, a transition between AC power and battery, or another change in the status of the system power supply.

• Suspend The operating system is about to be suspended.

SystemEvents.PowerModeChanged += OnPowerChange;

private void OnPowerChange(object s, PowerModeChangedEventArgs e) 
{
    switch ( e.Mode ) 
    {
        case PowerModes.Resume: 
        break;
        case PowerModes.Suspend:
        break;
    }
}

这篇关于如何检测当Windows 8进入睡眠状态或恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆