ManagementEventWatcher.WaitForNextEvent ManagementEventWatcher.WaitForNextEvent ManagementEventWatcher.WaitForNextEvent ManagementEventWatcher.WaitForNextEvent Method

정의

지정된 쿼리와 일치하는 다음 이벤트가 도착하기를 기다린 다음 해당 이벤트를 반환합니다.Waits for the next event that matches the specified query to arrive, and then returns it.

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

반환

새로 도착한 이벤트를 나타내는 ManagementBaseObject입니다.A ManagementBaseObject representing the newly arrived event.

예제

다음 예제에서는 클라이언트의 경우 인스턴스의 알림을 수신 하는 방법을 보여 줍니다 Win32_Process 이벤트 클래스 이므로 만들어집니다 __InstanceCreationEvent합니다.The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. 자세한 내용은 WMI(Windows Management Instrumentation) 설명서를 참조 하세요.For more information, see the Windows Management Instrumentation documentation. 클라이언트에서는 WaitForNextEvent 메서드를 호출하여 이벤트를 동기적으로 수신합니다.The client receives events synchronously by calling the WaitForNextEvent method. 예제 코드가 실행될 때 메모장과 같은 프로세스를 시작하여 이 예제를 테스트할 수 있습니다.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;
        // 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

설명

지정된 된 양의 시간 동안만 다음 이벤트에 대 한 API 대기는 지정된 된 제한 시간을 사용 하 여 옵션을 포함 하는 event watcher 개체, 경우 그렇지 않은 경우 API는 다음 이벤트가 발생할 때까지 차단 됩니다.If the event watcher object contains options with a specified time-out, the API will wait for the next event only for the specified amount of time; otherwise, the API will be blocked until the next event occurs.

.NET Framework 보안.NET Framework Security

직접 실행 호출자의 경우 완전히 신뢰합니다.Full trust for the immediate caller. 이 멤버는 부분적으로 신뢰할 수 있는 코드에서 사용할 수 없습니다.This member cannot be used by partially trusted code. 자세한 내용은 부분적으로 신뢰할 수 있는 코드에서 라이브러리를 사용 하 여입니다.For more information, see Using Libraries from Partially Trusted Code.

적용 대상