i trying catch batterystatuschange event in wmi. when execute program, not battery events @ either when battery starts charging or discharging.
code trying out:
namespace batterystatuschange { class program { static void main(string[] args) { try { wqleventquery query = new wqleventquery( "select * batterystatuschange"); managementscope scope = new managementscope(string.format("\\\\{0}\\root\\wmi", "localhost"), null); scope.connect(); managementeventwatcher watcher = new managementeventwatcher(scope, query); console.writeline("waiting event..."); managementbaseobject eventobj = watcher.waitfornextevent(); console.writeline("{0} event occurred.", eventobj["__class"]); // cancel event subscription watcher.stop(); return; } catch (managementexception err) { messagebox.show("an error occurred while trying receive event: " + err.message); } } } }
when execute program, not battery events @ either when battery starts charging or discharging.
i have tried different query strings 1 below no avail:
select * batterystatuschange poweronline = true
any appreciated.
try using batterystatus
wmi class instead.
try sample.
using system; using system.collections.generic; using system.management; using system.text; namespace getwmi_info { public class eventwatcherasync { private void wmieventhandler(object sender, eventarrivedeventargs e) { console.writeline("targetinstance.charging : " + ((managementbaseobject)e.newevent.properties["targetinstance"].value)["charging"]); console.writeline("targetinstance.description : " + ((managementbaseobject)e.newevent.properties["targetinstance"].value)["description"]); console.writeline("targetinstance.name : " + ((managementbaseobject)e.newevent.properties["targetinstance"].value)["name"]); console.writeline("targetinstance.poweronline : " + ((managementbaseobject)e.newevent.properties["targetinstance"].value)["poweronline"]); console.writeline("targetinstance.remainingcapacity : " + ((managementbaseobject)e.newevent.properties["targetinstance"].value)["remainingcapacity"]); console.writeline("targetinstance.voltage : " + ((managementbaseobject)e.newevent.properties["targetinstance"].value)["voltage"]); } public eventwatcherasync() { try { string computername = "localhost"; string wmiquery; managementeventwatcher watcher; managementscope scope; if (!computername.equals("localhost", stringcomparison.ordinalignorecase)) { connectionoptions conn = new connectionoptions(); conn.username = ""; conn.password = ""; conn.authority = "ntlmdomain:domain"; scope = new managementscope(string.format("\\\\{0}\\root\\wmi", computername), conn); } else scope = new managementscope(string.format("\\\\{0}\\root\\wmi", computername), null); scope.connect(); wmiquery ="select * __instancemodificationevent within 1 "+ "where targetinstance isa 'batterystatus' "; watcher = new managementeventwatcher(scope, new eventquery(wmiquery)); watcher.eventarrived += new eventarrivedeventhandler(this.wmieventhandler); watcher.start(); console.read(); watcher.stop(); } catch (exception e) { console.writeline("exception {0} trace {1}", e.message, e.stacktrace); } } public static void main(string[] args) { console.writeline("listening {0}", "__instancemodificationevent"); console.writeline("press enter exit"); eventwatcherasync eventwatcher = new eventwatcherasync(); console.read(); } } }
Comments
Post a Comment