Skip to Content
0

Run 1 script in each sessions

May 07, 2017 at 07:33 AM

83

avatar image
Former Member

Hello,

I want to run 1 script for each sessions open in my SAP GUI.
The first script is functionally, but the second wait the first script before to start.
I use multi-threading in C# to run those 2 script in parallel but it doesn't good.

Is it possible to run multiple script in parallel ? If yes, how to do that ?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Stefan Schnell
May 08, 2017 at 02:11 PM
0

Hello Corentin,

yes, it is possible. I tried it with PowerShell and the Job commands. At first I create a script which calls TAC SE16 and opens table TADIR ten times.

#-Begin-----------------------------------------------------------------

  #-Parameters----------------------------------------------------------
    Param($SessionNo)

  #-Includes------------------------------------------------------------
    ."C:\Dummy\COM.ps1"

  #-Main----------------------------------------------------------------
    $SapGuiAuto = Get-Object "SAPGUI"
    If ($SapGuiAuto -isnot [__ComObject]) {
      Exit
    }

    $application = Invoke-Method $SapGuiAuto "GetScriptingEngine"
    If ($application -isnot [__ComObject]) {
      Free-Object $SapGuiAuto
      Exit
    }

    $connection = Get-Property $application "Children"
    If ($connection -eq $Null) {
      Free-Object $SapGuiAuto
      Exit
    }
    If ($application.Children().Count() -gt 1) {
      $connection = $connection[0]
    }

    $session = Get-Property $connection "Children"
    If ($session -eq $Null) {
      Free-Object $SapGuiAuto
      Exit
    }
    If ($connection.Children().Count() -gt 1) {
      $session = $session[$SessionNo]
    }

  For ($i = 1; $i -le 10; $i++) {
    $ID = Invoke-Method $session "findById" @("wnd[0]/tbar[0]/okcd")
    Set-Property $ID "text" @("/nse16")
    $ID = Invoke-Method $session "findById" @("wnd[0]/tbar[0]/btn[0]")
    Invoke-Method $ID "press"
    Start-Sleep -s 1
    $ID = Invoke-Method $session "findById" @("wnd[0]/usr/ctxtDATABROWSE-TABLENAME")
    Set-Property $ID "text" @("TADIR")
    $ID = Invoke-Method $session "findById" @("wnd[0]/tbar[1]/btn[7]")
    Invoke-Method $ID "press"
    Start-Sleep -s 1
    $ID = Invoke-Method $session "findById" @("wnd[0]/tbar[1]/btn[8]")
    Invoke-Method $ID "press"
    $ID = Invoke-Method $session "findById" @("wnd[0]/tbar[0]/btn[3]")
    Invoke-Method $ID "press"
    Start-Sleep -s 1
    $ID = Invoke-Method $session "findById" @("wnd[0]/tbar[0]/btn[3]")
    Invoke-Method $ID "press"
    $ID = Invoke-Method $session "findById" @("wnd[0]/tbar[0]/btn[3]")
    Invoke-Method $ID "press"
  }

#-End-------------------------------------------------------------------

Then I started this script via start-job command two times and the sessions works peaceful side by side parallel.

#-Begin-----------------------------------------------------------------

start-job -Name j1 -FilePath C:\Dummy\test.ps1 -ArgumentList 0
start-job -Name j2 -FilePath C:\Dummy\test.ps1 -ArgumentList 1

wait-job -name j1
wait-job -name j2

receive-job -name j1
receive-job -name j2

#-End-------------------------------------------------------------------

I transfer the parameter session number in the AgrumentList parameter, in my case session 0 and 1 from connection 0.

I am no C# programmer, so I can't say how to do that in C# language.

Cheers
Stefan

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Stefan,

"and the sessions works peaceful side by side"- I liked that statement :-)

K.Kiran.

1