Aracılığıyla paylaş


ManagementEventWatcher.WaitForNextEvent Yöntem

Tanım

Belirtilen sorguyla eşleşen bir sonraki olayın gelmesini bekler ve ardından döndürür.

public:
 System::Management::ManagementBaseObject ^ WaitForNextEvent();
public System.Management.ManagementBaseObject WaitForNextEvent ();
member this.WaitForNextEvent : unit -> System.Management.ManagementBaseObject
Public Function WaitForNextEvent () As ManagementBaseObject

Döndürülenler

ManagementBaseObject Yeni gelen olayı temsil eden bir.

Örnekler

Aşağıdaki örnekte, olay sınıfı __InstanceCreationEvent olduğundan bir Win32_Process örneği oluşturulduğunda istemcinin nasıl bildirim aldığı gösterilmektedir. Daha fazla bilgi için Windows Yönetim Araçları belgelerine bakın. İstemci, yöntemini çağırarak WaitForNextEvent olayları zaman uyumlu olarak alır. Bu örnek, örnek kod çalışırken Not Defteri gibi bir işlem başlatılarak test edilebilir.

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

Açıklamalar

Olay izleyicisi nesnesi belirtilen zaman aşımına sahip seçenekler içeriyorsa, API bir sonraki olayı yalnızca belirtilen süre boyunca bekler; aksi takdirde, API bir sonraki olay gerçekleşene kadar engellenir.

.NET Framework Güvenliği

Anında arayanlar için tam güven. Bu üye kısmen güvenilen kodla kullanılamaz. Daha fazla bilgi için bkz. Kısmen Güvenilen Koddan Kitaplıkları Kullanma.

Şunlara uygulanır