Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Stefan-Schnell
Active Contributor
5,378
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.
2 Comments
Labels in this area