Share via


EventWatcherOptions Konstruktory

Definice

Inicializuje novou instanci EventWatcherOptions třídy pro sledování událostí.

Přetížení

EventWatcherOptions()

Inicializuje novou instanci EventWatcherOptions třídy pro sledování událostí pomocí výchozích hodnot. Toto je konstruktor bez parametrů.

EventWatcherOptions(ManagementNamedValueCollection, TimeSpan, Int32)

Inicializuje novou instanci EventWatcherOptions třídy s danými hodnotami.

EventWatcherOptions()

Zdroj:
ManagementOptions.cs
Zdroj:
ManagementOptions.cs
Zdroj:
ManagementOptions.cs

Inicializuje novou instanci EventWatcherOptions třídy pro sledování událostí pomocí výchozích hodnot. Toto je konstruktor bez parametrů.

public:
 EventWatcherOptions();
public EventWatcherOptions ();
Public Sub New ()

Příklady

Následující příklad ukazuje, jak klient obdrží oznámení při vytvoření instance Win32_Process , protože třída událostí je __InstanceCreationEvent. Další informace najdete v dokumentaci k nástroji Windows Management Instrumentation . Klient přijímá události synchronně voláním WaitForNextEvent metody. Tento příklad lze otestovat spuštěním procesu, například Poznámkového bloku, zatímco je ukázkový kód spuštěný.

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
        string query = "SELECT * FROM" +
            " __InstanceCreationEvent WITHIN 1 " +
            "WHERE TargetInstance isa \"Win32_Process\"";

        // Event options
        EventWatcherOptions eventOptions = new
            EventWatcherOptions();
        eventOptions.Timeout = System.TimeSpan.MaxValue;

        // Initialize an event watcher and subscribe to events
        // that match this query
        ManagementEventWatcher watcher =
            new ManagementEventWatcher("root\\CIMV2", query,
            eventOptions);

        // 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 String
        query = "SELECT * FROM" & _
            " __InstanceCreationEvent WITHIN 1 " & _
            "WHERE TargetInstance isa ""Win32_Process"""

        ' Event options
        Dim eventOptions As New EventWatcherOptions
        eventOptions.Timeout = System.TimeSpan.MaxValue

        ' Initialize an event watcher and subscribe to events 
        ' that match this query
        Dim watcher As New ManagementEventWatcher( _
            "root\CIMV2", query, eventOptions)

        ' 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

Poznámky

Zabezpečení rozhraní .NET Framework

Plná důvěra přímému volajícímu. Tento člen nemůže být použit částečně důvěryhodným kódem. Další informace najdete v tématu Použití knihoven z částečně důvěryhodného kódu.

Platí pro

EventWatcherOptions(ManagementNamedValueCollection, TimeSpan, Int32)

Zdroj:
ManagementOptions.cs
Zdroj:
ManagementOptions.cs
Zdroj:
ManagementOptions.cs

Inicializuje novou instanci EventWatcherOptions třídy s danými hodnotami.

public:
 EventWatcherOptions(System::Management::ManagementNamedValueCollection ^ context, TimeSpan timeout, int blockSize);
public EventWatcherOptions (System.Management.ManagementNamedValueCollection context, TimeSpan timeout, int blockSize);
new System.Management.EventWatcherOptions : System.Management.ManagementNamedValueCollection * TimeSpan * int -> System.Management.EventWatcherOptions
Public Sub New (context As ManagementNamedValueCollection, timeout As TimeSpan, blockSize As Integer)

Parametry

context
ManagementNamedValueCollection

Kontextový objekt options obsahující informace specifické pro zprostředkovatele, které se mají předávat zprostředkovateli.

timeout
TimeSpan

Časový limit čekání na další události

blockSize
Int32

Počet událostí, na které se mají čekat v každém bloku.

Příklady

Následující příklad ukazuje, jak klient obdrží oznámení při vytvoření instance Win32_Process , protože třída událostí je __InstanceCreationEvent. Další informace najdete v dokumentaci k nástroji Windows Management Instrumentation . Klient přijímá události synchronně voláním WaitForNextEvent metody. Tento příklad lze otestovat spuštěním procesu, například Poznámkového bloku, zatímco je ukázkový kód spuštěný.

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
        string query = "SELECT * FROM" +
            " __InstanceCreationEvent WITHIN 1 " +
            "WHERE TargetInstance isa \"Win32_Process\"";

        // Event options
        // blockSize = 1 to receive one event
        EventWatcherOptions eventOptions = new
            EventWatcherOptions(null, System.TimeSpan.MaxValue,
            1);

        // Initialize an event watcher and subscribe to events
        // that match this query
        ManagementEventWatcher watcher =
            new ManagementEventWatcher("root\\CIMV2", query,
            eventOptions);

        // 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 String
        query = "SELECT * FROM" & _
            " __InstanceCreationEvent WITHIN 1 " & _
            "WHERE TargetInstance isa ""Win32_Process"""

        ' Event options
        ' blockSize = 1 to receive one event
        Dim eventOptions As New EventWatcherOptions( _
            Nothing, System.TimeSpan.MaxValue, 1)

        ' Initialize an event watcher and subscribe to events 
        ' that match this query
        Dim watcher As New ManagementEventWatcher( _
            "root\CIMV2", query, eventOptions)

        ' 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

Poznámky

Zabezpečení rozhraní .NET Framework

Plná důvěra přímému volajícímu. Tento člen nemůže být použit částečně důvěryhodným kódem. Další informace najdete v tématu Použití knihoven z částečně důvěryhodného kódu.

Platí pro