SAP GUI Scripting: Collection vs. Children, the Busy Difference
In some cases it is necessary with SAP GUI Scripting to loop over all existing connections and sessions. If a session is busy, which means the SAP GUI is waiting for data from the server, it is necessary to detect this situation, to avoid blocking the execution thread. For this case offers SAP GUI Scripting the property Busy of the GuiSession class. But in some cases blocks the property Children of the GuiConnection class, before it is possible to use Busy property.
The following code blocks the execution at line
$session = Get-Property $connection "Children" @($j)
The execution of the script is blocked until the session is available again.
#-Begin-----------------------------------------------------------------
#-Includes------------------------------------------------------------
."$PSScriptRoot\COM.ps1"
#-Sub Main------------------------------------------------------------
Function Main() {
$SapGuiAuto = Get-Object( , "SAPGUI")
If ($SapGuiAuto -isnot [__ComObject]) {
Exit
}
$application = Invoke-Method $SapGuiAuto "GetScriptingEngine"
If ($application -isnot [__ComObject]) {
Free-Object $SapGuiAuto
Exit
}
For($i = 0; $i -lt $application.Children().Count(); $i++) {
$connection = Get-Property $application "Children" @($i)
For($j = 0; $j -lt $connection.Children().Count(); $j++) {
$session = Get-Property $connection "Children" @($j)
If ($session.Busy() -eq 1) {
Continue
}
}
}
Free-Object $SapGuiAuto
}
#-Main----------------------------------------------------------------
Main
#-End-------------------------------------------------------------------
Here another approach to bypass this situation:
#-Begin-----------------------------------------------------------------
#-Includes------------------------------------------------------------
."$PSScriptRoot\COM.ps1"
#-Sub Main------------------------------------------------------------
Function Main() {
$SapGuiAuto = Get-Object( , "SAPGUI")
If ($SapGuiAuto -isnot [__ComObject]) {
Exit
}
$application = Invoke-Method $SapGuiAuto "GetScriptingEngine"
If ($application -isnot [__ComObject]) {
Free-Object $SapGuiAuto
Exit
}
$connections = Get-Property $application "Connections"
ForEach ($connection In $connections) {
$sessions = Get-Property $connection "Sessions"
ForEach ($session In $sessions) {
If ($session.Busy() -eq 1) {
Continue
}
}
}
Free-Object $SapGuiAuto
}
#-Main----------------------------------------------------------------
Main
#-End-------------------------------------------------------------------
Both sources do exactly the same, they loop over all connections and sessions. But in the secound source I use another method. I get all connections and sessions and loop over this. And with this approach works the Busy property as expected.
Hello Stefan,
I have been trying to do a script to run multiple transactions in different windows, I found this article https://archive.sap.com/discussions/thread/3388231 which is already archived since it is the year 2013 now reading this topic on the property “Busy”, would it be possible to run multiple sessions simultaneously?
Note: My scripts are in VBA or Autoit.
Thank you very much
Márcio.
Hello Márcio,
sure it that possible. Here an example in PowerShell.
The ScriptBlock contains the script and with the commands Start-Job two scripts are running parallel. PowerShell offers fantastic possibilities to use background jobs. In my eyes it is not up to date anymore to realize those kinds of solutions in VBA. So you must realize solutions which have nothing to do with your business problem. To use this solution also in VBA you can use SAPIENS ActiveXPoSHV3, PowerShell access for VBScript/JScript developers - but you can use it also in VBA or AutoIt. You can find more information here. Here an example how to use it in VBA:
Cheers
Stefan