EventWatcherOptions Construtores
Definição
Inicializa uma nova instância da classe EventWatcherOptions para inspeção de eventos.Initializes a new instance of the EventWatcherOptions class for event watching.
Sobrecargas
| EventWatcherOptions() |
Inicializa uma nova instância da classe EventWatcherOptions para inspeção de eventos usando os valores padrão.Initializes a new instance of the EventWatcherOptions class for event watching, using default values. Esse é o construtor sem parâmetros.This is the parameterless constructor. |
| EventWatcherOptions(ManagementNamedValueCollection, TimeSpan, Int32) |
Inicializa uma nova instância da classe EventWatcherOptions com os valores fornecidos.Initializes a new instance of the EventWatcherOptions class with the given values. |
EventWatcherOptions()
Inicializa uma nova instância da classe EventWatcherOptions para inspeção de eventos usando os valores padrão.Initializes a new instance of the EventWatcherOptions class for event watching, using default values. Esse é o construtor sem parâmetros.This is the parameterless constructor.
public:
EventWatcherOptions();
public EventWatcherOptions ();
Public Sub New ()
Exemplos
O exemplo a seguir mostra como o cliente recebe a notificação quando uma instância do Win32_Process é criada porque a classe de evento é __InstanceCreationEvent.The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. Para obter mais informações, consulte a documentação do Instrumentação de gerenciamento do Windows .For more information, see the Windows Management Instrumentation documentation. O cliente recebe eventos de forma síncrona chamando o WaitForNextEvent método.The client receives events synchronously by calling the WaitForNextEvent method. Este exemplo pode ser testado com o início de um processo, como o bloco de notas, enquanto o código de exemplo está em execução.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
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
Comentários
Segurança do .NET Framework.NET Framework Security
Confiança total para o chamador imediato.Full trust for the immediate caller. Este membro não pode ser usado pelo código parcialmente confiável.This member cannot be used by partially trusted code. Para obter mais informações, consulte usando bibliotecas de código parcialmente confiável.For more information, see Using Libraries from Partially Trusted Code.
Aplica-se a
EventWatcherOptions(ManagementNamedValueCollection, TimeSpan, Int32)
Inicializa uma nova instância da classe EventWatcherOptions com os valores fornecidos.Initializes a new instance of the EventWatcherOptions class with the given values.
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)
Parâmetros
- context
- ManagementNamedValueCollection
O objeto de contexto de opções que contém informações específicas de provedor a serem passadas para o provedor.The options context object containing provider-specific information to be passed through to the provider.
- timeout
- TimeSpan
O tempo limite de espera para os próximos eventos.The time-out to wait for the next events.
- blockSize
- Int32
O número de eventos a aguardar em cada bloco.The number of events to wait for in each block.
Exemplos
O exemplo a seguir mostra como o cliente recebe a notificação quando uma instância do Win32_Process é criada porque a classe de evento é __InstanceCreationEvent.The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. Para obter mais informações, consulte a documentação do Instrumentação de gerenciamento do Windows .For more information, see the Windows Management Instrumentation documentation. O cliente recebe eventos de forma síncrona chamando o WaitForNextEvent método.The client receives events synchronously by calling the WaitForNextEvent method. Este exemplo pode ser testado com o início de um processo, como o bloco de notas, enquanto o código de exemplo está em execução.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
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
Comentários
Segurança do .NET Framework.NET Framework Security
Confiança total para o chamador imediato.Full trust for the immediate caller. Este membro não pode ser usado pelo código parcialmente confiável.This member cannot be used by partially trusted code. Para obter mais informações, consulte usando bibliotecas de código parcialmente confiável.For more information, see Using Libraries from Partially Trusted Code.