ManagementEventWatcher Class

 

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

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

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

Public Class ManagementEventWatcher
	Inherits Component

NameDescription
System_CAPS_pubmethodManagementEventWatcher()

Initializes a new instance of the ManagementEventWatcher class. For further initialization, set the properties on the object. This is the default constructor.

System_CAPS_pubmethodManagementEventWatcher(EventQuery)

Initializes a new instance of the ManagementEventWatcher class when given a WMI event query.

System_CAPS_pubmethodManagementEventWatcher(ManagementScope, EventQuery)

Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query.

System_CAPS_pubmethodManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions)

Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query, according to the specified options. For this variant, the query and the scope are specified objects. The options object can specify options such as time-out and context information.

System_CAPS_pubmethodManagementEventWatcher(String)

Initializes a new instance of the ManagementEventWatcher class when given a WMI event query in the form of a string.

System_CAPS_pubmethodManagementEventWatcher(String, String)

Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query. For this variant, the query and the scope are specified as strings.

System_CAPS_pubmethodManagementEventWatcher(String, String, EventWatcherOptions)

Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query, according to the specified options. For this variant, the query and the scope are specified as strings. The options object can specify options such as a time-out and context information.

NameDescription
System_CAPS_protpropertyCanRaiseEvents

Gets a value indicating whether the component can raise an event.(Inherited from Component.)

System_CAPS_pubpropertyContainer

Gets the IContainer that contains the Component.(Inherited from Component.)

System_CAPS_protpropertyDesignMode

Gets a value that indicates whether the Component is currently in design mode.(Inherited from Component.)

System_CAPS_protpropertyEvents

Gets the list of event handlers that are attached to this Component.(Inherited from Component.)

System_CAPS_pubpropertyOptions

Gets or sets the options used to watch for events.

System_CAPS_pubpropertyQuery

Gets or sets the criteria to apply to events.

System_CAPS_pubpropertyScope

Gets or sets the scope in which to watch for events (namespace or scope).

System_CAPS_pubpropertySite

Gets or sets the ISite of the Component.(Inherited from Component.)

NameDescription
System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDispose()

Releases all resources used by the Component.(Inherited from Component.)

System_CAPS_protmethodDispose(Boolean)

Releases the unmanaged resources used by the Component and optionally releases the managed resources.(Inherited from Component.)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Ensures that outstanding calls are cleared. This is the destructor for the object. In C#, finalizers are expressed using destructor syntax. (Overrides Component.Finalize().)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodGetService(Type)

Returns an object that represents a service provided by the Component or by its Container.(Inherited from Component.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodStart()

Subscribes to events with the given query and delivers them, asynchronously, through the EventArrived event.

System_CAPS_pubmethodStop()

Cancels the subscription whether it is synchronous or asynchronous.

System_CAPS_pubmethodToString()

Returns a String containing the name of the Component, if any. This method should not be overridden.(Inherited from Component.)

System_CAPS_pubmethodWaitForNextEvent()

Waits for the next event that matches the specified query to arrive, and then returns it.

NameDescription
System_CAPS_pubeventDisposed

Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)

System_CAPS_pubeventEventArrived

Occurs when a new event arrives.

System_CAPS_pubeventStopped

Occurs when a subscription is canceled.

The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. For more information, see the Windows Management Instrumentation documentation in the MSDN Library at http://msdn.microsoft.com/library. The client receives events synchronously by calling the WaitForNextEvent method. This example can be tested by starting a process, such as Notepad, while the example code is running.

Imports System
Imports System.Management

' This example shows synchronous consumption of events. 
' The client is blocked while waiting for events. 

Public Class EventWatcherPolling
    Public Overloads Shared Function _
        Main(ByVal args() As String) As Integer

        ' Create event query to be notified within 1 second of 
        ' a change in a service
        Dim query As New WqlEventQuery( _
            "__InstanceCreationEvent", _
            New TimeSpan(0, 0, 1), _
            "TargetInstance isa ""Win32_Process""")

        ' Initialize an event watcher and subscribe to events 
        ' that match this query
        Dim watcher As New ManagementEventWatcher
        watcher.Query = query
        ' times watcher.WaitForNextEvent in 5 seconds
        watcher.Options.Timeout = New TimeSpan(0, 0, 5)

        ' Block until the next event occurs 
        ' Note: this can be done in a loop
        ' if waiting for more than one occurrence
        Console.WriteLine( _
          "Open an application (notepad.exe) to trigger an event.")
        Dim e As ManagementBaseObject = _
            watcher.WaitForNextEvent()

        'Display information from the event
        Console.WriteLine( _
            "Process {0} has created, path is: {1}", _
            CType(e("TargetInstance"), _
                ManagementBaseObject)("Name"), _
            CType(e("TargetInstance"), _
                ManagementBaseObject)("ExecutablePath"))

        'Cancel the subscription
        watcher.Stop()
        Return 0

    End Function 'Main
End Class 'EventWatcherPolling

.NET Framework
Available since 1.1

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

Return to top
Show: