Advanced COM Execute method
Execute method

Requests execution of a method over the object passed from the COMThread. The thread must be active and not currently busy executing other method. If these conditions are not met an error will occur. The method returns after the method is executed.


object.Execute objectToExec, method


objectToExec - An object created earlier.

method - String containing the method name. It will be called through IDispatch on the objecttoExec.


See the example on the COMApartment page 


The method parameter is a string - the name of a method of the object to execute. The method must have no arguments. If it returns a value it can be read after completing the execution from the Result property. However the returned value should be non-object value if possible - for example strings, numbers, VBArrays of simple types will be ok for sure, but a collection or file wrapping object (SFStream, FSO's file) may cause problems in some hosts. 

If you are executing a script in the thread note that it will be better the return value is not an in-script object (like the JScript arrays - unlike the arrays in VBScript they are objects). If an in-script value is returned or otherwise passed to the main thread (through the ASP Application for example) it will be accessible/alive as long as the script host used to execute the script is alive and script in it is not unloaded/stopped. This will limit the scope where the value can be accessed or at least will require additional attention - to keep the host alive after completing the actual work with it. Apparently this can be a source for human mistakes during the further development. 

Thread should not be released (go out of scope in script applications) until it is busy. To achieve that you can do what is appropriate for your application - for example:

  • Save the thread object (COMThread), the executed object and its apartment (if used - see COMApartment) in global scope variables (such as Application in ASP) and then check them later (in ASP you will probably want to check them from another page/further request)
  • When everything else is done (the work your application does after executing the method) you can Wait the execution to complete.

In many cases the main thread (that creates the COMThread object) and the executed method are somehow related e.g. the main program may need the results from the thread at some point (after completing some other tasks concurrently). In such case Wait-ing the execution to complete is what you will need to do in order to continue after the thread is ready.

We strongly recommend using COMApartment with the COMThread. The best and safest way to run threads from a scripting application is to create an apartment, create the object that will be executed there and then pass it to the thread. This does not require you to know anything in depth for the object, the host or COM ingeneral (i.e. it requires a few more lines but is extremely easy). Passing an object created in the main thread (using the internal script's CreateObject functions or host's functions) may cause deadlock depending on the host's and the object's specifics. To determine when COMApartment usage can be avoided you will need thorough COM knowledge and deep knowledge about the hosting program (ALP, IIS, WSH etc.) and the executed object itself (not only its members but also its internal implementation specifics). Therefore the best way is to use COMApartment and avoid spending time on learning details you may need only once. For those who still want to break this scheme we want to remind that if the host and the objects come from other vendors you cannot be sure that they will not change and what is possible now can be impossible with their future versions.   

Applies to: COMthread object

newObjects Copyright 2001-2006 newObjects [ ]