ManagementEventWatcher.Scope ManagementEventWatcher.Scope ManagementEventWatcher.Scope ManagementEventWatcher.Scope Property

Definizione

Ottiene o imposta l'ambito nel quale eseguire il controllo degli eventi, ossia lo spazio dei nomi o l'ambito.Gets or sets the scope in which to watch for events (namespace or scope).

public:
 property System::Management::ManagementScope ^ Scope { System::Management::ManagementScope ^ get(); void set(System::Management::ManagementScope ^ value); };
public System.Management.ManagementScope Scope { get; set; }
member this.Scope : System.Management.ManagementScope with get, set
Public Property Scope As ManagementScope

Valore della proprietà

L'ambito in cui il controllo degli eventi.The scope in which to watch for events.

Esempi

Nell'esempio seguente viene illustrato il client riceve una notifica quando un'istanza del Win32_Process viene creato perché è la classe di 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. Per altre informazioni, vedere la Windows Management Instrumentation documentazione.For more information, see the Windows Management Instrumentation documentation. Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent.The client receives events synchronously by calling the WaitForNextEvent method. Questo esempio può essere testato avviando un processo, ad esempio Blocco note, mentre il codice di esempio è in esecuzione.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;
        watcher.Scope = new ManagementScope("root\\CIMV2");
        // 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
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
        watcher.Scope = New ManagementScope("root\CIMV2")
        ' 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 'EventWatcherPolling

Commenti

Valore proprietàProperty Value

L'ambito in cui il controllo degli eventi (spazio dei nomi o ambito).The scope in which to watch for events (namespace or scope).

Sicurezza di .NET Framework.NET Framework Security

Attendibilità totale per il chiamante immediato.Full trust for the immediate caller. Impossibile usare questo membro in codice parzialmente attendibile.This member cannot be used by partially trusted code. Per altre informazioni, vedere utilizzo di librerie da codice parzialmente attendibile.For more information, see Using Libraries from Partially Trusted Code.

Si applica a