Edit

ManagementEventWatcher Class

Definition

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

public ref class ManagementEventWatcher : System::ComponentModel::Component
public class ManagementEventWatcher : System.ComponentModel.Component
type ManagementEventWatcher = class
    inherit Component
Public Class ManagementEventWatcher
Inherits Component
Inheritance
ManagementEventWatcher

Examples

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. 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.

using System;
using System.Management;

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

public class EventWatcherPolling
{
    public static int Main(string[] args)
    {
        // Create event query to be notified within 1 second of
        // a change in a service
        WqlEventQuery query =
            new WqlEventQuery("__InstanceCreationEvent",
            new TimeSpan(0,0,1),
            "TargetInstance isa \"Win32_Process\"");

        // Initialize an event watcher and subscribe to events
        // that match this query
        ManagementEventWatcher watcher =
            new ManagementEventWatcher();
        watcher.Query = query;
        // times out 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.");
        ManagementBaseObject e = watcher.WaitForNextEvent();

        //Display information from the event
        Console.WriteLine(
            "Process {0} has been created, path is: {1}",
            ((ManagementBaseObject)e
            ["TargetInstance"])["Name"],
            ((ManagementBaseObject)e
            ["TargetInstance"])["ExecutablePath"]);

        //Cancel the subscription
        watcher.Stop();
        return 0;
    }
}
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

Constructors

Name Description
ManagementEventWatcher()

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

ManagementEventWatcher(EventQuery)

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

ManagementEventWatcher(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.

ManagementEventWatcher(ManagementScope, EventQuery)

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

ManagementEventWatcher(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.

ManagementEventWatcher(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.

ManagementEventWatcher(String)

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

Properties

Name Description
Options

Gets or sets the options used to watch for events.

Query

Gets or sets the criteria to apply to events.

Scope

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

Methods

Name Description
Finalize()

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

Start()

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

Stop()

Cancels the subscription whether it is synchronous or asynchronous.

WaitForNextEvent()

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

Events

Name Description
EventArrived

Occurs when a new event arrives.

Stopped

Occurs when a subscription is canceled.

Applies to