Skip to Content

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.

Enjoy it.

Cheers
Stefan

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply