Collecting Results During Asynchronous Execution

Since Visual Basic is not multithreaded, there are no callbacks for results collection. The collectNext function is used for collecting results during asynchronous calls. The function returns one of the available results from the Service; if none are available, it waits for the number of milliseconds supplied as an input argument before returning. The timeout prevents a client application from waiting indefinitely on a Service.

collectNext returns the result as a ServiceData type, which includes the following properties:

Id — The Service invocation ID, which can be used to match the result with the corresponding submission.
IsError — A Boolean that signifies whether the execution resulted in an error; if so, the data property contains the error message.
Data — The result (Service function returns value or error).

For the same reason as execution and submit functions, all output arguments are pure strings.

To make collecting multiple results easier, the InvocationCount parameter of a Service is equal to the number of outstanding invocations.

The following code demonstrates how one can implement results collection. It repeatedly checks a Service for available results. If there are results, one result is collected; the code checks to see if the result is an error, and prints out the result data accordingly. If there are no results, it waits for 1 second, and then loops. The loop ends when all the results have been collected.

While service.InvocationCount > 0
Dim sd As ServiceData
Set sd = service.collectNext(1000)
If Not sd Is Nothing Then
If sd.IsError Then
Debug.Print "collected " & sd.Id & " error: " & sd.Data
Else
Debug.Print "collected " & sd.Id & ": " & sd.Data
End If
End If
Wend
Debug.Print "done collecting"