Rich Andrews
Rich Andrews

Reputation: 4188

How can I react to windows waking from sleep mode in a wpf application?

I have a simple messaging wpf application that listens to a wcf duplex service to receive messages. I have coded it so that if the network fails (or I disconnect the LAN cable) it reconnects to the service which works well.

My problem is, when Windows goes into sleep mode it fails to try to reconnect. I suspect this is because my timer for polling the network is put to sleep and therefore the polling stops.

So, is there a way to react to a "Windows has woken up" event or similar?

Upvotes: 2

Views: 3216

Answers (2)

Rich Andrews
Rich Andrews

Reputation: 4188

I finally found what i wanted, a simple, managed code way to react to the system resume event...

Microsoft.Win32.SystemEvents.PowerModeChanged += this.SystemEvents_PowerModeChanged;

private void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
{
    if (e.Mode == PowerModes.Resume)
        {
            //Do Some processing here
        }
}

The (major) benefit of this approach over p/invoke is that ir works across OS's (I need no extra handling for Windows XP & Vista) and, of course, it's rather more consice!

Upvotes: 3

Pateman
Pateman

Reputation: 2757

Have you seen http://www.codeproject.com/KB/system/OSEvents.aspx ?

It is coded in C++, but I believe it uses only Win32API, so using P/Invoke you should be able to use the code in your application. :)

Upvotes: 0

Related Questions