Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

ManagementEventWatcher Class

Subscribes to temporary event notifications based on a specified event query.

For a list of all members of this type, see ManagementEventWatcher Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Management.ManagementEventWatcher

[Visual Basic]
Public Class ManagementEventWatcher
   Inherits Component
[C#]
public class ManagementEventWatcher : Component
[C++]
public __gc class ManagementEventWatcher : public Component
[JScript]
public class ManagementEventWatcher extends Component

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Example

[C#] 
using System; 
using System.Management; 

// This example demonstrates how to subscribe to an event using the ManagementEventWatcher object. 
class Sample_ManagementEventWatcher 
{ 
    public static int Main(string[] args) { 

        //For the example, we'll put a class into the repository, and watch
        //for class deletion events when the class is deleted.
        ManagementClass newClass = new ManagementClass(); 
        newClass["__CLASS"] = "TestDeletionClass";
        newClass.Put();

        //Set up an event watcher and a handler for the event
        ManagementEventWatcher watcher = new ManagementEventWatcher(
            new WqlEventQuery("__ClassDeletionEvent"));
        MyHandler handler = new MyHandler();
        watcher.EventArrived += new EventArrivedEventHandler(handler.Arrived);

        //Start watching for events
        watcher.Start();
      
        // For the purpose of this sample, we delete the class to trigger the event
        // and wait for two seconds before terminating the consumer
        newClass.Delete();
       
        System.Threading.Thread.Sleep(2000);
 
        //Stop watching
        watcher.Stop();
       
        return 0;
    }
 
    public class MyHandler {
        public void Arrived(object sender, EventArrivedEventArgs e) {
            Console.WriteLine("Class Deleted = " +
               ((ManagementBaseObject)e.NewEvent["TargetClass"])["__CLASS"]);
        }
    }
}
   
[Visual Basic] 
Imports System
Imports System.Management

' This example demonstrates how to subscribe an event using the ManagementEventWatcher object.
Class Sample_ManagementEventWatcher
    Public Shared Sub Main()

        ' For the example, we'll put a class into the repository, and watch
        ' for class deletion events when the class is deleted.
        Dim newClass As New ManagementClass()
        newClass("__CLASS") = "TestDeletionClass"
        newClass.Put()
        
        ' Set up an event watcher and a handler for the event
        Dim watcher As _
            New ManagementEventWatcher(New WqlEventQuery("__ClassDeletionEvent"))
        Dim handler As New MyHandler()
        AddHandler watcher.EventArrived, AddressOf handler.Arrived
 
        ' Start watching for events
        watcher.Start()

        ' For the purpose of this sample, we delete the class to trigger the event
        ' and wait for two seconds before terminating the consumer      
        newClass.Delete()

        System.Threading.Thread.Sleep(2000)
  
        ' Stop watching
        watcher.Stop()

    End Sub

    Public Class MyHandler
        Public Sub Arrived(sender As Object, e As EventArrivedEventArgs)
            Console.WriteLine("Class Deleted = " & _
                CType(e.NewEvent("TargetClass"), ManagementBaseObject)("__CLASS"))
        End Sub
    End Class
End Class
   

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Management

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Management (in System.Management.dll)

See Also

ManagementEventWatcher Members | System.Management Namespace

Show:
© 2014 Microsoft. All rights reserved.