Python WMI Event Handler - Consumer

D

Dips

Python Gurus,

Is it possible to write python WMI event consumer using event handler
rather than polling WMI?


I got the equivalent C# code snippet from
http://www.ondotnet.com/pub/a/dotnet/2003/04/07/wmi.html

using System;
using System.Management;
using System.Collections;

class WSMonitor
{
bool pleaseContinue = true;

public void doit()
{
try
{

int processId =
System.Diagnostics.Process.GetCurrentProcess().Id;
int workingSet = 30000000;
string wqlQuery = String.Format(
@"SELECT * FROM __InstanceModificationEvent WITHIN 1
WHERE TargetInstance ISA 'Win32_Process' AND
TargetInstance.ProcessId = {0} AND
TargetInstance.WorkingSetSize >= {1} AND
PreviousInstance.WorkingSetSize < {2} ",
processId, workingSet, workingSet);

WqlEventQuery query = new WqlEventQuery(wqlQuery);
ManagementEventWatcher watcher =
new ManagementEventWatcher(query);
watcher.EventArrived +=
new EventArrivedEventHandler(onEvent);

watcher.Start();
ArrayList array = new ArrayList();
for (int i = 0; pleaseContinue; ++i)
{
array.Add(1);

if (i % 1000 == 0)
System.Threading.Thread.Sleep(1);
}

watcher.Stop();
}
catch (ManagementException e)
{
Console.WriteLine("Management exception: " + e);
}
}

public void onEvent(object sender, EventArrivedEventArgs e)
{
pleaseContinue = false;
Console.WriteLine("You're a big boy now!");
}
}

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top