Python WMI Event Handler - Consumer

Discussion in 'Python' started by Dips, Aug 5, 2003.

  1. Dips

    Dips Guest

    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
    Dips, Aug 5, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sonic
    Replies:
    1
    Views:
    535
    John Saunders
    Jan 7, 2005
  2. Replies:
    1
    Views:
    696
    Damien
    Feb 22, 2007
  3. RC

    How to recall add event from an Event handler??

    RC, Jan 6, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    260
    John Saunders
    Jan 6, 2005
  4. Adi
    Replies:
    2
    Views:
    192
  5. Replies:
    0
    Views:
    143
Loading...

Share This Page