Making a Semisynchronous Call with VBScript
Some WMI methods can return large collections, causing scripts to stop responding. In script, semisynchronous access is the default, and Windows Management Instrumentation (WMI) sets wbemFlagReturnImmediately for calls that can return large object collections such as the following SWbemServices methods: InstancesOf, SubclassesOf, ExecQuery, AssociatorsOf, and ReferencesTo.
Semisynchronous access that uses wbemFlagReturnImmediately set in the IFlags parameter is also the default for calls that can return large object sets for the following SWbemObject methods: Instances_, Subclasses_, Associators_, and References_.
To reduce the WMI memory resource usage when processing a large collection of objects, include the value of wbemFlagForwardOnly in the IFlags parameter. Using wbemFlagForwardOnly causes WMI to create a forward-only enumerator that does not allow rewinding the collection and accessing items again.
WMI eliminates the memory for each object as the For Each statement processes an object. You cannot call the Count method for a collection when the wbemFlagForwardOnly flag was set on the call that obtained the collection. Note that the IFlags parameter has wbemFlagReturnImmediately and wbemFlagForwardOnly set by default for the SWbemServices.ExecNotificationQuery method.
The following procedure describes how to use VBScript to make a semisynchronous call.
To make a semisynchronous call in VBScript
- Set the IFlags parameter to the value of wbemFlagReturnImmediately.
- Make the normal synchronous call for SWbemServices.ExecQuery or SWbemServices.ExecNotificationQuery with the iFlags value.
- If you want to treat the objects returned by the call as a collection, use an enumeration syntax such as VBScript For Each. As each object is returned, it is processed as the next item in the collection.
- Create a forward-only enumerator by combining the value of wbemFlagReturnImmediately with the value of wbemFlagForwardOnly. The decimal value of this OR operation is 48. These constants are defined in the wbemdisp.tlb type library for Visual Basic. Most scripting languages use the numeric value or define a constant. For more information, see WbemFlagEnum.
The following code example shows how to make a semisynchronous method call. For more information, see Calling a Method.
wbemFlagReturnImmediately = 16 wbemFlagForwardOnly = 32 IFlags = wbemFlagReturnImmediately + wbemFlagForwardOnly WScript.Echo IFlags Set objWMIService = GetObject("winmgmts:root\cimv2") ' Query for all the Win32_Process objects on the ' local computer and use forward-only enumerator Set colProcesses = objWMIService.ExecQuery("SELECT Name FROM Win32_Process",,IFlags) ' Receive each object as it arrives For Each objProcess in colProcesses WScript.Echo objProcess.Name Next