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.

Aa392301.wedge(en-us,VS.85).gifTo make a semisynchronous call in VBScript

  1. Set the IFlags parameter to the value of wbemFlagReturnImmediately.
  2. Make the normal synchronous call for SWbemServices.ExecQuery or SWbemServices.ExecNotificationQuery with the iFlags value.
  3. 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.
  4. 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

Related topics

Calling a Method