ManagementEventWatcher Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy ManagementEventWatcher.

Przeciążenia

ManagementEventWatcher()

Inicjuje nowe wystąpienie klasy ManagementEventWatcher. Aby kontynuować inicjowanie, ustaw właściwości obiektu. Jest to konstruktor bez parametrów.

ManagementEventWatcher(EventQuery)

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, gdy zostanie podane zapytanie o zdarzenie WMI.

ManagementEventWatcher(String)

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, gdy podane zapytanie o zdarzenie WMI w postaci ciągu.

ManagementEventWatcher(ManagementScope, EventQuery)

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, która nasłuchuje zdarzeń zgodnych z danym zapytaniem zdarzenia usługi WMI.

ManagementEventWatcher(String, String)

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, która nasłuchuje zdarzeń zgodnych z danym zapytaniem zdarzenia usługi WMI. W przypadku tego wariantu zapytanie i zakres są określane jako ciągi.

ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions)

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, która nasłuchuje zdarzeń zgodnych z danym zapytaniem zdarzenia usługi WMI, zgodnie z określonymi opcjami. W przypadku tego wariantu zapytanie i zakres są określonymi obiektami. Obiekt options może określać opcje, takie jak limit czasu i informacje kontekstowe.

ManagementEventWatcher(String, String, EventWatcherOptions)

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, która nasłuchuje zdarzeń zgodnych z danym zapytaniem zdarzenia usługi WMI, zgodnie z określonymi opcjami. W przypadku tego wariantu zapytanie i zakres są określane jako ciągi. Obiekt options może określać opcje, takie jak limit czasu i informacje kontekstowe.

ManagementEventWatcher()

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

Inicjuje nowe wystąpienie klasy ManagementEventWatcher. Aby kontynuować inicjowanie, ustaw właściwości obiektu. Jest to konstruktor bez parametrów.

public:
 ManagementEventWatcher();
public ManagementEventWatcher ();
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
        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

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

ManagementEventWatcher(EventQuery)

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

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, gdy zostanie podane zapytanie o zdarzenie WMI.

public:
 ManagementEventWatcher(System::Management::EventQuery ^ query);
public ManagementEventWatcher (System.Management.EventQuery query);
new System.Management.ManagementEventWatcher : System.Management.EventQuery -> System.Management.ManagementEventWatcher
Public Sub New (query As EventQuery)

Parametry

query
EventQuery

Reprezentujące EventQuery zapytanie zdarzenia usługi WMI, które określa zdarzenia, dla których obserwator będzie nasłuchiwać.

Przykłady

W tym przykładzie kodu 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\"";

        // Initialize an event watcher and subscribe to events
        // that match this query
        ManagementEventWatcher watcher =
            new ManagementEventWatcher(new EventQuery(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 String
        query = "SELECT * FROM __InstanceCreationEvent " _
            & "WITHIN 1 WHERE " & _
            "TargetInstance isa ""Win32_Process"""

        ' Initialize an event watcher and subscribe to events 
        ' that match this query
        Dim watcher As New ManagementEventWatcher(New EventQuery( _
        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

Uwagi

Przestrzeń nazw, w której obserwator będzie nasłuchiwać zdarzeń, to domyślna przestrzeń nazw, która jest obecnie ustawiona.

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

ManagementEventWatcher(String)

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

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, gdy podane zapytanie o zdarzenie WMI w postaci ciągu.

public:
 ManagementEventWatcher(System::String ^ query);
public ManagementEventWatcher (string query);
new System.Management.ManagementEventWatcher : string -> System.Management.ManagementEventWatcher
Public Sub New (query As String)

Parametry

query
String

Zapytanie zdarzenia usługi WMI, które definiuje zdarzenia, dla których obserwator będzie nasłuchiwać.

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\"";

        // Initialize an event watcher and subscribe to events
        // that match this query
        ManagementEventWatcher watcher =
            new ManagementEventWatcher(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 String
        query = "SELECT * FROM __InstanceCreationEvent " _
            & "WITHIN 1 WHERE " & _
            "TargetInstance isa ""Win32_Process"""

        ' Initialize an event watcher and subscribe to events 
        ' that match this query
        Dim watcher As New ManagementEventWatcher(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

Uwagi

Przestrzeń nazw, w której obserwator będzie nasłuchiwać zdarzeń, to domyślna przestrzeń nazw, która jest obecnie ustawiona.

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

ManagementEventWatcher(ManagementScope, EventQuery)

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

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, która nasłuchuje zdarzeń zgodnych z danym zapytaniem zdarzenia usługi WMI.

public:
 ManagementEventWatcher(System::Management::ManagementScope ^ scope, System::Management::EventQuery ^ query);
public ManagementEventWatcher (System.Management.ManagementScope scope, System.Management.EventQuery query);
new System.Management.ManagementEventWatcher : System.Management.ManagementScope * System.Management.EventQuery -> System.Management.ManagementEventWatcher
Public Sub New (scope As ManagementScope, query As EventQuery)

Parametry

scope
ManagementScope

Reprezentujący ManagementScope zakres (przestrzeń nazw), w którym obserwator będzie nasłuchiwać zdarzeń.

query
EventQuery

Reprezentujące EventQuery zapytanie zdarzenia usługi WMI, które określa zdarzenia, dla których obserwator będzie nasłuchiwać.

Przykłady

W tym przykładzie kodu 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\"";

        // Initialize an event watcher and subscribe to events
        // that match this query
        ManagementEventWatcher watcher =
            new ManagementEventWatcher(
            new ManagementScope("root\\CIMV2"),
            new EventQuery(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 String
        query = "SELECT * FROM __InstanceCreationEvent " _
            & "WITHIN 1 WHERE " & _
            "TargetInstance isa ""Win32_Process"""

        ' Initialize an event watcher and subscribe to events 
        ' that match this query
        Dim watcher As New ManagementEventWatcher( _
            New ManagementScope("root\CIMV2"), _
            New EventQuery(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

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

ManagementEventWatcher(String, String)

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

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, która nasłuchuje zdarzeń zgodnych z danym zapytaniem zdarzenia usługi WMI. W przypadku tego wariantu zapytanie i zakres są określane jako ciągi.

public:
 ManagementEventWatcher(System::String ^ scope, System::String ^ query);
public ManagementEventWatcher (string scope, string query);
new System.Management.ManagementEventWatcher : string * string -> System.Management.ManagementEventWatcher
Public Sub New (scope As String, query As String)

Parametry

scope
String

Zakres zarządzania (przestrzeń nazw), w którym obserwator będzie nasłuchiwać zdarzeń.

query
String

Zapytanie definiujące zdarzenia, dla których obserwator będzie nasłuchiwać.

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\"";

        // Initialize an event watcher and subscribe to events
        // that match this query
        ManagementEventWatcher watcher =
            new ManagementEventWatcher("root\\CIMV2",
            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 String
        query = "SELECT * FROM __InstanceCreationEvent " _
            & "WITHIN 1 WHERE " & _
            "TargetInstance isa ""Win32_Process"""

        ' Initialize an event watcher and subscribe to events 
        ' that match this query
        Dim watcher As New ManagementEventWatcher( _
            "root\CIMV2", 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

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

ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions)

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

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, która nasłuchuje zdarzeń zgodnych z danym zapytaniem zdarzenia usługi WMI, zgodnie z określonymi opcjami. W przypadku tego wariantu zapytanie i zakres są określone obiekty. Obiekt options może określać opcje, takie jak limit czasu i informacje kontekstowe.

public:
 ManagementEventWatcher(System::Management::ManagementScope ^ scope, System::Management::EventQuery ^ query, System::Management::EventWatcherOptions ^ options);
public ManagementEventWatcher (System.Management.ManagementScope scope, System.Management.EventQuery query, System.Management.EventWatcherOptions options);
new System.Management.ManagementEventWatcher : System.Management.ManagementScope * System.Management.EventQuery * System.Management.EventWatcherOptions -> System.Management.ManagementEventWatcher
Public Sub New (scope As ManagementScope, query As EventQuery, options As EventWatcherOptions)

Parametry

scope
ManagementScope

Reprezentujący ManagementScope zakres (przestrzeń nazw), w którym obserwator będzie nasłuchiwać zdarzeń.

query
EventQuery

Reprezentujące EventQuery zapytanie zdarzenia usługi WMI, które określa zdarzenia, dla których obserwator będzie nasłuchiwać.

options
EventWatcherOptions

Reprezentujące EventWatcherOptions dodatkowe opcje używane do watch dla zdarzeń.

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, so wait for 1 event to return
        EventWatcherOptions options = new EventWatcherOptions(
            null, TimeSpan.MaxValue, 1);

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

        // 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, so wait for 1 event to return
        Dim options As New EventWatcherOptions( _
            Nothing, TimeSpan.MaxValue, 1)


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

        ' 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

ManagementEventWatcher(String, String, EventWatcherOptions)

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

Inicjuje ManagementEventWatcher nowe wystąpienie klasy, która nasłuchuje zdarzeń zgodnych z danym zapytaniem zdarzenia usługi WMI, zgodnie z określonymi opcjami. W przypadku tego wariantu zapytanie i zakres są określane jako ciągi. Obiekt options może określać opcje, takie jak limit czasu i informacje kontekstowe.

public:
 ManagementEventWatcher(System::String ^ scope, System::String ^ query, System::Management::EventWatcherOptions ^ options);
public ManagementEventWatcher (string scope, string query, System.Management.EventWatcherOptions options);
new System.Management.ManagementEventWatcher : string * string * System.Management.EventWatcherOptions -> System.Management.ManagementEventWatcher
Public Sub New (scope As String, query As String, options As EventWatcherOptions)

Parametry

scope
String

Zakres zarządzania (przestrzeń nazw), w którym obserwator będzie nasłuchiwać zdarzeń.

query
String

Zapytanie definiujące zdarzenia, dla których obserwator będzie nasłuchiwać.

options
EventWatcherOptions

Reprezentująca EventWatcherOptions dodatkowe opcje używane do watch dla zdarzeń.

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, so wait for 1 event to return
        EventWatcherOptions options = new EventWatcherOptions(
            null, TimeSpan.MaxValue, 1);

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

        // 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, so wait for 1 event to return
        Dim options As New EventWatcherOptions( _
            Nothing, TimeSpan.MaxValue, 1)


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

        ' 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