Event to detect System wake up from sleep in C#

前端 未结 3 786
温柔的废话
温柔的废话 2020-11-27 16:29

I need to detect the system power state mode. To be precise, I need an event which fires up when windows 7 wakes up from sleep. I am already using:

SystemEve         


        
相关标签:
3条回答
  • 2020-11-27 16:40
    SystemEvents.PowerModeChanged += OnPowerChange;
    
    private void OnPowerChange(object s, PowerModeChangedEventArgs e) 
    {
        switch ( e.Mode ) 
        {
            case PowerModes.Resume: 
            break;
            case PowerModes.Suspend:
            break;
        }
    }
    

    You should probably read this: http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.powermodechanged.aspx

    0 讨论(0)
  • 2020-11-27 16:48

    You need to inspect the Mode property of the PowerModeChangedEventArgs that is passed to the event.

    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.

    0 讨论(0)
  • 2020-11-27 16:58
    SystemEvents.PowerModeChanged += OnPowerModeChange;
    private void OnPoweModerChange(object s, PowerModeChangedEventArgs e)
    {
      if(e.Mode==PowerModes.Suspend)
      {
        //Apply your operation
      }
    }
    

    Use this code to do your job

    0 讨论(0)
提交回复
热议问题