Reflection Desktop VBA Guide
Key Concepts / Handling Asynchronous Behavior
In This Topic
    Handling Asynchronous Behavior
    In This Topic

    When your macro interacts with a mainframe program, you'll need to handle the asynchronous behavior that occurs between your macro and the host program.

    Your macro runs independently of the host program. Two programs are processsing commands (the host program and your macro) and these programs are not synchronized. Because of this asyncronous behaviour, you'll need to pause execution of your macro after you send a command to the host. After the host responds and Reflection processes the data to render a new screen, you can resume execution. If your macro tries to interact with the screen before it is ready, it will get unexpected results or errors.

    To handle this asychronous behaviour and make sure your macro waits until the terminal screens or Web pages are ready, use the following Reflection events and methods.

    For IBM programs, use the NewScreenReady event to handle asynchronous behaviour.

    Using the NewScreenReady Event
    Copy Code
    Private Sub IbmScreen_NewScreenReady(ByVal sender As Variant)
      'Set the Screen ID variables used to identify which screen is active.
      Dim ScreenID1 As String
      Dim ScreenID2 As String
      Dim retVal As ReturnCode
      'Get values for the screen IDs every time a screen loads.
      ScreenID1 = Trim(ThisIbmScreen.GetText(1, 2, 3))
      ScreenID2 = Trim(ThisIbmScreen.GetText(1, 1, 5))
      'Compare the ScreenID values with known values to determine which screen is active.
      If ScreenID1 = "ATM" Then
         'Send command to host
         ThisIbmScreen.SendControlKey (ControlKeyCode_Transmit)
      ElseIf ScreenID2 = "LOGON" Or ScreenID2 = "Ready" Then
         'Send data to host
         ThisIbmScreen.SendKeys ("ISPF")
         'Send command to host
         ThisIbmScreen.SendControlKey (ControlKeyCode_Transmit)
      End If
    End Sub


    For Open Systems, Reflection provides several WaitForString methods that you can use to make sure the host has finished its reply before you resume execution. The WaitForStrings methods wait for specific strings to be sent from the host. As soon as the final string in a host response has arrived, you can continue execution of your macro.

    To set up your macro, record it and navigate through the screens you need to use. Then look at the recorded macro to find the strings that the WaitForString method is waiting for. In this example, we'll use the WaitForString3 method to wait for a screen in the Reflection demo to settle after we send a command. 

    To set up your macro to wait for the host reply

    1. Open Reflection and start a VT session. (for our example, we use a demo session with a Host name / IP Address of "demo:UNIX".)
    2. On the log in screen, enter any credentials for user name and password.
    3. On the Tools tab, in the Macros group, click Record Macro.
    4. Perform the actions you want to automate with your macro. (In the example, at the demo prompt, enter "demodata".)
    5. On the Tools tab, click Stop Recording.
    6. In the Recording Complete dialog box, save the recorded macro in a session project and then open the Visual Basic Editor.
    7. In the Visual Basic Project window, open the Recorded module for this session and find the strings referenced in the WaitForString3 method.   
      Get the strings to wait for
      Copy Code
      'Wait for a string on the host screen before continuing
      returnValue = osCurrentScreen.WaitForString3(LF & "Command> ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    8. Specify to wait for these strings in your macro, as shown below:
      Using WaitForString3
      Copy Code
      Dim returnValue As Integer
      Const NEVER_TIME_OUT = 0
      ThisScreen.SendKeys "demodata"
      ThisScreen.SendControlKey ControlKeyCode_Return
      'Wait for a string on the host screen before continuing
      returnValue = osCurrentScreen.WaitForString3(LF & "Command> ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
      'Continue with commands

    For Web documents, you can use the DocumentCompleted event to handle aynchronous behaviour.

    Using the DocumentCompleted event
    Copy Code
    Private Sub WebControl_DocumentCompleted(ByVal sender As Variant, ByVal Url As String)
      Dim wElementValue As WebElement
      Dim wElementArray() As WebElement
      'Get the heading text of the page
      wElementArray = ThisWebControl.Document.GetElementsByTagName("h1")
      'Search the heading text to find out if the browser is on the page with the form 
      If InStr(1, wElementArray(0).InnerHtml, "download instructions") > 0 Then
        'If on the page with the form, put an email in the Email form field
        Set wElementValue = ThisWebControl.Document.GetElementById("emailAddress")
        wElementValue.PutText ("")
      End If
    End Sub






    See Also