Udostępnij przez


WqlEventQuery.WithinInterval Właściwość

Definicja

Pobiera lub ustawia interwał sondowania do użycia w tym zapytaniu.

public:
 property TimeSpan WithinInterval { TimeSpan get(); void set(TimeSpan value); };
public TimeSpan WithinInterval { get; set; }
member this.WithinInterval : TimeSpan with get, set
Public Property WithinInterval As TimeSpan

Wartość właściwości

Zwraca wartość zawierającą TimeSpan interwał sondowania używany w zapytaniu zdarzenia.

Przykłady

Poniższy przykład inicjuje nowy WqlEventQuery , który reprezentuje zapytanie: "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'".

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 new process being created
        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(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 seconds of 
        ' a process being created
        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(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

Tę właściwość należy ustawić tylko w przypadkach, gdy nie ma dostawcy zdarzeń dla żądanego zdarzenia, a usługa WMI jest wymagana do sondowania żądanego warunku.

Ustawienie tej wartości właściwości zastępuje dowolną poprzednią wartość przechowywaną w obiekcie. Ciąg zapytania jest ponownie utworzony w celu odzwierciedlenia nowego interwału.

Wartość właściwości

Wartość null, jeśli nie ma udziału sondowania; w przeciwnym razie prawidłowa TimeSpan wartość, jeśli sondowanie jest wymagane.

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