Power Loss Events in Windows 7 ... Question C #

I am working on a C # WPF application that will be on a Windows 7 computer with an APC 1500 UPS connected. If I lose power, I need an application to respond to certain shutdown actions before Windows 7 shuts it down.

Can I directly access Windows events or do I need to interact with APC software? If Windows, what events? Any links or information would be appreciated - I just didn't see much when I was looking.

Thanks.

+4
source share
2 answers

Perhaps this can 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(); } 
+1
source

You can try just catch WM_QUERYENDSESSION. MSDN has a good sample code for this .

I understand that in Windows Vista / 7 it is no longer possible to infinitely delay a shutdown or pause. However, I believe that the system will give your process a reasonable amount of time to do something before you finish it. (Someone please correct me if I am wrong about this.)

Please note that this only helps you disconnect. If you need information about the notification and management on the UPS itself, I'm not sure how to do this.

0
source

All Articles