Waits for the next event that matches the specified query to arrive, and then returns it.
Assembly: System.Management (in System.Management.dll)
Public Function WaitForNextEvent As ManagementBaseObject
public ManagementBaseObject WaitForNextEvent()
public:
ManagementBaseObject^ WaitForNextEvent()
member WaitForNextEvent : unit -> ManagementBaseObject
Return Value
Type: System.Management.ManagementBaseObjectA ManagementBaseObject representing the newly arrived event.
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 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.
The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. For more information, see the Windows Management Instrumentation documentation in the MSDN Library at http://msdn.microsoft.com/library. 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.
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 ' 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
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; } }
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1- 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.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.