EventWatcherOptions Konstruktory

Definicja

Inicjuje EventWatcherOptions nowe wystąpienie klasy do oglądania zdarzeń.

Przeciążenia

EventWatcherOptions()

Inicjuje EventWatcherOptions nowe wystąpienie klasy do oglądania zdarzeń przy użyciu wartości domyślnych. Jest to konstruktor bez parametrów.

EventWatcherOptions(ManagementNamedValueCollection, TimeSpan, Int32)

Inicjuje EventWatcherOptions nowe wystąpienie klasy z podanymi wartościami.

EventWatcherOptions()

Źródło:
ManagementOptions.cs
Źródło:
ManagementOptions.cs
Źródło:
ManagementOptions.cs

Inicjuje EventWatcherOptions nowe wystąpienie klasy do oglądania zdarzeń przy użyciu wartości domyślnych. Jest to konstruktor bez parametrów.

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

Przykłady

W poniższym przykładzie pokazano, jak klient odbiera powiadomienie po utworzeniu wystąpienia Win32_Process , ponieważ klasa zdarzeń jest __InstanceCreationEvent. Aby uzyskać więcej informacji, zobacz dokumentację instrumentacji zarządzania windows . Klient odbiera zdarzenia synchronicznie przez wywołanie WaitForNextEvent metody . Ten przykład można przetestować, uruchamiając proces, taki jak Notatnik, podczas gdy przykładowy kod jest uruchomiony.

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

Uwagi

Zabezpieczenia.NET Framework

Pełne zaufanie do bezpośredniego wywołującego. Ten element członkowski nie może być używany przez kod częściowo zaufany. Aby uzyskać więcej informacji, zobacz Using Libraries from Partially Trusted Code (Używanie bibliotek z częściowo zaufanego kodu).

Dotyczy

EventWatcherOptions(ManagementNamedValueCollection, TimeSpan, Int32)

Źródło:
ManagementOptions.cs
Źródło:
ManagementOptions.cs
Źródło:
ManagementOptions.cs

Inicjuje EventWatcherOptions nowe wystąpienie klasy z podanymi wartościami.

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

Obiekt kontekstu opcji zawierający informacje specyficzne dla dostawcy, które mają być przekazywane do dostawcy.

timeout
TimeSpan

Limit czasu oczekiwania na następne zdarzenia.

blockSize
Int32

Liczba zdarzeń, które będą czekać w każdym bloku.

Przykłady

W poniższym przykładzie pokazano, jak klient odbiera powiadomienie po utworzeniu wystąpienia Win32_Process , ponieważ klasa zdarzeń jest __InstanceCreationEvent. Aby uzyskać więcej informacji, zobacz dokumentację instrumentacji zarządzania windows . Klient odbiera zdarzenia synchronicznie przez wywołanie WaitForNextEvent metody . Ten przykład można przetestować, uruchamiając proces, taki jak Notatnik, podczas gdy przykładowy kod jest uruchomiony.

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

Uwagi

Zabezpieczenia.NET Framework

Pełne zaufanie do bezpośredniego wywołującego. Ten element członkowski nie może być używany przez kod częściowo zaufany. Aby uzyskać więcej informacji, zobacz Using Libraries from Partially Trusted Code (Używanie bibliotek z częściowo zaufanego kodu).

Dotyczy