4

I'm working on an C# WPF application that will be on a Windows 7 machine with an APC 1500 UPS attached. On power loss, I need the app to respond with certain shutdown activities before Windows 7 shuts it down.

Can I deal directly with windows events or do I need to interact with the APC software? If Windows, what events? Any links or info would be appreciated - I just haven't seen much when searching.

Thanks.

Ender
  • 41
  • 3

2 Answers2

1

Perhaps this could help?

 private ManagementEventWatcher managementEventWatcher;
    private readonly Dictionary<string, string> powerValues = new Dictionary<string, string>
                         {
                             {"4", "Entering Suspend"},
                             {"7", "Resume from Suspend"},
                             {"10", "Power Status Change"},
                             {"11", "OEM Event"},
                             {"18", "Resume Automatic"}
                         };
    public void InitPowerEvents()
    {
        var q = new WqlEventQuery();
        var scope = new ManagementScope("root\\CIMV2");

        q.EventClassName = "Win32_PowerManagementEvent";
        managementEventWatcher = new ManagementEventWatcher(scope, q);
        managementEventWatcher.EventArrived += PowerEventArrive;
        managementEventWatcher.Start();
    }
    private void PowerEventArrive(object sender, EventArrivedEventArgs e)
    {
        foreach (PropertyData pd in e.NewEvent.Properties)
        {
            if (pd == null || pd.Value == null) continue;
            var name = powerValues.ContainsKey(pd.Value.ToString())
                           ? powerValues[pd.Value.ToString()]
                           : pd.Value.ToString();
            Console.WriteLine("PowerEvent:"+name);
        }
    }
    public void Stop()
    {
        managementEventWatcher.Stop();
    }
Emmanuel
  • 7,574
  • 3
  • 24
  • 22
0

You might try simply catching WM_QUERYENDSESSION. MSDN has some good example code for this.

It is my understanding that in Windows Vista/7, it is no longer possible to indefinitely delay a shutdown or suspend. However, I do believe the system will give your process a reasonable amount of time to do things before ending it. (Someone, please correct me if I am wrong on this point.)

Note that this only helps you upon shut down. If you need notification and management info on the UPS itself, then I am unsure on how to dot this.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • In Win7 the user gets the option to forceably shut down a process if it is taking too long, so you should always treat data saved in response to this event as unreliable. – slugster Nov 14 '10 at 04:21
  • @slugster, any idea how long before the user is given this option? – Brad Nov 14 '10 at 16:07
  • i don't know the exact time, i just know from having to use the feature. If the user doesn't exercise the option then Win7 continues waiting, although i'm not sure how long it will wait if something is truly hung as opposed to just taking a while. – slugster Nov 15 '10 at 09:39