Detect system "Wake Up" from Sleep/Hibernate/Stand-By

Event handler available? System environment variable?

Page 1 of 1

5 Replies - 28594 Views - Last Post: 01 June 2010 - 07:35 AM Rate Topic: -----

#1 KeyboardKowboy   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 142
  • Joined: 15-December 08

Detect system "Wake Up" from Sleep/Hibernate/Stand-By

Posted 28 May 2010 - 01:32 PM

I'm assuming something like this must have come up before, and am hoping someone can point me in the right direction. I need to find a way to check the system or be alerted to the fact that the physical machine has recently been "woken" from a low power state.

I have an NTP (Network Time Protocol) Client class within a project I am working on. The class is utilized whenever I need to synchronize an internal DateTime object within my system. The problem is that when certain systems go into hibernate or sleep mode (stand-by), it enters into a low power state and typically disables/power's down the NIC card. When the system is "awoken" and the user quickly attempts to run a function within the system before the NIC has had a chance to reset, there is typically a connection problem.

I would like to avoid the issue by being able to detect that the system has recently awoke from a low power state and in the scenario where a user quickly attempts to utilize the NIC, the method thread that makes the call will sleep for 10 seconds or so to allow the NIC to catch up.

Anyone have any ideas? Would this be an event that can be handled? Are there any .NET system variables that can point out this scenario? I don't need anything to be super specific, just a broad generalization that the machine had recently awoken would be good enough. Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Detect system "Wake Up" from Sleep/Hibernate/Stand-By

#2 elbielefeld   User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 217
  • Joined: 18-May 10

Re: Detect system "Wake Up" from Sleep/Hibernate/Stand-By

Posted 28 May 2010 - 02:13 PM

Hi,

have a look at the SystemEvents.PowerModeChanged event.
Was This Post Helpful? 1
  • +
  • -

#3 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Detect system "Wake Up" from Sleep/Hibernate/Stand-By

Posted 28 May 2010 - 02:17 PM

You should be able to use the GetSystemPowerStatus Win32 API to detect when a computer comes out of hibernation/stand-by. You can also look at the SystemInformation.PowerStatus Property as well
Was This Post Helpful? 2
  • +
  • -

#4 KeyboardKowboy   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 142
  • Joined: 15-December 08

Re: Detect system "Wake Up" from Sleep/Hibernate/Stand-By

Posted 29 May 2010 - 08:06 AM

Thanks for the tips guys. I'll look into these in more depth when I get back to work next week. I'll post back with my findings and results.
Was This Post Helpful? 0
  • +
  • -

#5 EvLSnoopY   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 93
  • Joined: 24-November 09

Re: Detect system "Wake Up" from Sleep/Hibernate/Stand-By

Posted 30 May 2010 - 04:32 AM

This is just something I was messing around with to find out if the power cable to my laptop was plugged in or not using those classes.
using System;
using System.Reflection;

static void Main(string[] args)
{
    Type t = typeof(System.Windows.Forms.PowerStatus);// t is now of type PowerStatus.
    PropertyInfo[] status = t.GetProperties(); // Returns all the properties of the current type.
    bool isTrue = true;
    
    while(isTrue)
    {
        // If the power cable is plugged in...
        if ( status[0].GetValue(SystemInformation.PowerStatus, null).Equals(PowerLineStatus.Online) )
        {
            // Then the PowerStatus is online.
            Console.WriteLine(status[0].Name + " is online." + Environment.NewLine);
            Console.ReadKey();
        }
        else
        {
            // If not then the PowerStatus is offline.
            Console.WriteLine(status[0].Name + " is offline." + Environment.NewLine);
            //isTrue = false; Optional; to get out of infinite loop.
            Console.ReadKey();
        }
    }
}


This post has been edited by EvLSnoopY: 30 May 2010 - 04:33 AM

Was This Post Helpful? 1
  • +
  • -

#6 KeyboardKowboy   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 142
  • Joined: 15-December 08

Re: Detect system "Wake Up" from Sleep/Hibernate/Stand-By

Posted 01 June 2010 - 07:35 AM

Thanks for the help everyone. What I really needed was a way to detect that the machine went into a low power state at some point. The SystemEvents PowerModeChanged Event Handler was exactly what i needed. It properly fires whenever the machine goes into or comes out of a low power state.

The other information posted concerning the System PowerStatus stuff was also good to run through as I may be utilizing some of that at a later date. Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1