Freigeben über


Virtual Server COM API - VMTask usage and error handling in VBscript

Virtual Server tasks are used to track and obtain status of asynchronous actions of COM methods. There are many Virtual Server COM API methods that return a VMTask interface so that the progress of the task can be tracked. The simplest usage of the VMTask interface is to define a variable to hold a VMTask object reference and assign the VMTask return value from a method. Once you have that reference you can then call an VMTask method like WaitForCompletion to wait for the asynchronous task to complete.

Dim objTask

 

Set objTask = someinterface.method()

objTask.WaitforCompletion(10000)

 

If objTask.IsComplete Then

   WScript.Echo "Task Successful"

Else

   WScript.Echo "Task still not Complete"

End If

The example code demonstrates the dimension and assignment of the objTask variable the task from the someinterface.method()method. The WaitForCompletion method is then called with a value of 10000 milliseconds. The script will wait for one of two actions to happen:

1. The task completes within 10 seconds and the method returns early

2. The task does not complete within 10 seconds and the method exits.

In the first case, the IsComplete method would return a True, in the second case the IsComplete method would return a False.

But you have tried this in your script and you get an error "object reference not found" on the Set objTask = someinterface.method() line. This is probably happening because there was an error in the someinterface.method()call and the VMTask reference was never returned and assigned to objTask. This results in an error in the script everytime you attempt to use the objTask methods or look at its properties. To solve this problem, modify the script slightly by adding Error handling using On Error Resume Next and checking to be sure that the objTask object reference contains a value before you attempt to use it. You accomplish this using the "Nothing" keyword in an object reference check.

Dim objTask

 

On Error Resume Next

Set objTask = someinterface.method()

If objTask Is Nothing Then
WScript.Echo "Error:" & Err.Description
Err.Clear
Else
objTask.WaitforCompletion(10000)

If objTask.IsComplete Then

WScript.Echo "Task Successful"

Else

WScript.Echo "Task still not Complete"

Wscript.Echo "Task Percent Remaining = " & (100 - objTask.PercentCompleted)

End If

End If

 I also added the feature to tell you if the task is not completed, what percentage is left to complete.

Well this is an intro to Task usage in Virtual Server scripts....hope you find it useful.

Comments

  • Anonymous
    January 01, 2003
    Robert Larson , co-author of the Virtual Server 2005 R2 Resource Kit, has joined the blogosphere. Gate