Reflection .NET API
Walkthroughs / Other Automation Tasks / Work With Multiple Sessions
In This Topic
    Work With Multiple Sessions
    In This Topic

    You can work with more than one session at a time in Reflection. This sample shows how to get text from one session and put it into another session.

    To copy text from one session to another 

    1. In Visual Studio, create a new Console Application project and add references for the following Reflection assemblies. (Depending on your version of Visual Studio, these can be found either on the .NET tab or under Assemblies | Extensions.)
      Attachmate.Reflection
      Attachmate.Reflection.Framework
      Attachmate.Reflection.Emulation.IbmHosts
      Attachmate.Reflection.Emulation.OpenSystems

    2. Replace all the code in the Program.cs file with the following code for the terminal you are using. This code sets up a property for the text that is copied from one session to another. It also creates two sessions and creates event handlers (and a method for Open Systems) to navigate through each session.
      Add property and create terminals
      Copy Code
      using System;
      using System.Threading;
      using Attachmate.Reflection.Framework;
      using Attachmate.Reflection.Emulation.IbmHosts;
      using Attachmate.Reflection.UserInterface;
      
      namespace WorkWithSessions
      {
          class MultipleSessions
          {
              //The value to copy between screens
              static private string _textToCopy = string.Empty;
              static public string TextToCopy
              {
                  get
                  {
                      return _textToCopy;
                  }
                  set
                  {
                      _textToCopy = value;
                  }
              }
      
          
              static void Main(string[] args)
              {
                  //Start a visible instance of Reflection or get the instance running at the given channel name
                  Application app = MyReflection.CreateApplication("myWorkspace", true);
      
                  //Create an IBM 5250 terminal control
                  IIbmTerminal terminalA = (IIbmTerminal)app.CreateControl(new Guid("{AF03A446-F278-4624-B9EF-C896DF2CA1CA}"));
      
                  //Create an IBM 3270 terminal control
                  IIbmTerminal terminalB = (IIbmTerminal)app.CreateControl(new Guid("{09E5A1B4-0BA6-4546-A27D-FE4762B7ACE1}"));
               
                  //Get the screens for each terminal
                  IIbmScreen screenA = terminalA.Screen;
                  IIbmScreen screenB = terminalB.Screen;
      
                  //Use the host addresses for the Reflection onboard demos
                  terminalA.HostAddress = "demo:ibm5250.sim";
                  terminalB.HostAddress = "demo:ibm3270.sim";
      
                  //Make the sessions visible                
                  IFrame frame = (IFrame)app.GetObject("Frame");
                  frame.CreateView(terminalA);
                  frame.CreateView(terminalB);
      
                  //Create event handlers to handle navigation for each screen
                  screenA.NewScreenReady += screenA_NewScreenReady;
                  screenB.NewScreenReady += screenB_NewScreenReady;
      
                  Console.ReadKey();
              }
          }
      }
      
      Add property and create terminals
      Copy Code
      using System;
      using System.Threading;
      using Attachmate.Reflection.Framework;
      using Attachmate.Reflection.Emulation.IbmHosts;
      using Attachmate.Reflection.Emulation.OpenSystems;
      using Attachmate.Reflection.UserInterface;
      using Attachmate.Reflection.Productivity;
      using ScreenPoint = Attachmate.Reflection.Emulation.OpenSystems.ScreenPoint;
      using IBMControlKeyCode = Attachmate.Reflection.Emulation.IbmHosts.ControlKeyCode;
      using OSControlKeyCode = Attachmate.Reflection.Emulation.OpenSystems.ControlKeyCode;
      
      namespace WorkWithSessions
      {
          class MultipleSessions
          {
              //The value to copy between screens
              static private string _textToCopy = string.Empty;
      
              static public string TextToCopy
              {
                  get
                  {
                      return _textToCopy;
                  }
                  set
                  {
                      _textToCopy = value;
                  }
              }
            
              static void Main(string[] args)
              {
                  //Start a visible instance of Reflection or get the instance running at the given channel name
                  Application app = MyReflection.CreateApplication("myWorkspace", true);
      
                  //Create an IBM 3270 terminal control and assign the Reflection onboard demo as the host address
                  IIbmTerminal terminalIBM = (IIbmTerminal)app.CreateControl(new Guid("{09E5A1B4-0BA6-4546-A27D-FE4762B7ACE1}"));
                  terminalIBM.HostAddress = "demo:ibm3270.sim";
      
                  //Open an Open Systems session and get a handle to the terminal control
                  string sessionPath = Environment.GetEnvironmentVariable("UserProfile") + @"\Documents\Micro Focus\Reflection\demoSession.rdox";
                  ITerminal terminalOS = (ITerminal)app.CreateControl(sessionPath);
      
                  //Get the screens for each terminal
                  IIbmScreen screenIBM = terminalIBM.Screen;
                  IScreen screenOS = terminalOS.Screen;
      
                  //Make the sessions visible                
                  IFrame frame = (IFrame)app.GetObject("Frame");
                  frame.CreateView(terminalOS);
                  frame.CreateView(terminalIBM);
                      
                  //Create an event handler to handle navigation for the IBM screen
                  screenIBM.NewScreenReady += screenIBM_NewScreenReady;
      
                  //Navigate to the Open Systems screen with the data and get the data
                  GetScreenData(screenOS);
      
                  Console.ReadKey();
              }
          }
      }
      
    3. Add the code to navigate through the first session and get some text.  
      Navigate to screen in first session and get text
      Copy Code
      //Navigate to the appropriate screen and set the text for the TextToCopy property
      static void screenA_NewScreenReady(object sender, EventArgs e)
      {
          IIbmScreen screen = (IIbmScreen)sender;
      
          //Get text strings for screen positions and compare these strings to
          //known values for each screen to determine which screen the program is on 
          string ScreenID1 = screen.GetText(1, 36, 4);
          string ScreenID2 = screen.GetText(1, 40, 4);
          string ScreenID3 = screen.GetText(1, 25, 13);
          if (ScreenID1 == "Sign")
          {
              screen.SendControlKey(ControlKeyCode.Transmit);
          }
          if (ScreenID2 == "Main")
          {
              screen.SendKeys("kayak");
              screen.SendControlKey(ControlKeyCode.Transmit);
          }
          if (ScreenID3 == "INTERNATIONAL")
          {
                     
              //Get the text and assign it to the TextToCopy property
              TextToCopy = screen.GetText(1, 39, 5);
          }
      }
      
      Navigate to screen in Open Systems session and get text
      Copy Code
      static void GetScreenData(IScreen screenOS)
      {
                 
          //Enter the user name
          screenOS.WaitForHostSettle(2000, 3000);
          screenOS.SendKeys("user");
          screenOS.WaitForHostSettle(2000, 3000);
          screenOS.SendControlKey(OSControlKeyCode.Return);
          screenOS.WaitForHostSettle(2000, 3000);
      
          //Prompt for a password
          ITerminal terminalOS = screenOS.Parent;
          string password = terminalOS.Macro.PasswordBox("Enter any password", "OS Password");
          screenOS.SendKeys(password);
          screenOS.WaitForHostSettle(2000, 3000, true);
          screenOS.SendControlKey(OSControlKeyCode.Return);
      
          //navigate to the screen with the data
          screenOS.WaitForHostSettle(2000, 3000, false);
          screenOS.SendKeys("demodata");
          screenOS.SendControlKey(OSControlKeyCode.Return);
          screenOS.WaitForHostSettle(2000, 3000, true);
          screenOS.SendControlKey(OSControlKeyCode.Return);
      
          //Find the row and column on the screen to get the data from
          ScreenPoint pointForRow = screenOS.SearchText("Jan", 1, 1, FindOptions.Forward);
          ScreenPoint pointForCol = screenOS.SearchText("Profit", 1, 1, FindOptions.Forward);
      
          //Get the data
          TextToCopy = screenOS.GetText(pointForRow.Row, pointForCol.Column + 1, 5);
      }
      
    4. Add the code to navigate through the second session and enter the text.  
      Navigate to screen in second session and enter text
      Copy Code
      //Navigate to the appropriate screen and put the text in the TextToCopy property on that screen
      static void screenB_NewScreenReady(object sender, EventArgs e)
      {
          IIbmScreen screen = (IIbmScreen)sender;
      
          //Get text strings for screen positions and compare these strings to
          //known values for each screen to determine which screen the program is on 
          string ScreenID1 = screen.GetText(1,2,3);
          string ScreenID2 = screen.GetText(1, 1, 5);
          string ScreenID3 = screen.GetText(2, 2, 6);
          string ScreenID4 = screen.GetText(2, 2, 7);
      
          if (ScreenID1 == "ATM") {
              screen.SendControlKey(ControlKeyCode.Transmit);
          }
          if (ScreenID2 == "LOGON")
          {
              screen.SendKeys("ISPF");
              screen.SendControlKey(ControlKeyCode.Transmit);
          }
          if (ScreenID3 == "OPTION")
          {
              screen.SendKeys("1");
              screen.SendControlKey(ControlKeyCode.Transmit);
          }
          if (ScreenID4 == "COMMAND")
          {
              //Make sure the text is retrieved from the other screen
              while (string.IsNullOrEmpty(TextToCopy))
              {                     
                  System.Threading.Thread.Sleep(500);
              } 
      
              //Put the text in field and reset the TextToCopy property
              screen.PutText(TextToCopy, 5, 18);
              TextToCopy = string.Empty;
          }
           
      }
      
      Navigate to screen in IBM session and enter text
      Copy Code
      //Navigate to the appropriate screen and put the text in the TextToCopy property on that screen
      static void screenIBM_NewScreenReady(object sender, EventArgs e)
      {
          IIbmScreen screen = (IIbmScreen)sender;
      
          //Get text strings for screen positions and compare these strings to
          //known values for each screen to determine which screen the program is on 
          string ScreenID1 = screen.GetText(1, 2, 3);
          string ScreenID2 = screen.GetText(1, 1, 5);
          string ScreenID3 = screen.GetText(2, 2, 6);
          string ScreenID4 = screen.GetText(2, 2, 7);
      
          if (ScreenID1 == "ATM")
          {
                     
              //Enter the user name
              screen.PutText("userName", 20, 16);
                     
              //No password is required for the IBM demo - Do not hard code a password
              //Use a password store or get the password from user input
              screen.SendControlKey(IBMControlKeyCode.Transmit);
          }
          if (ScreenID2 == "LOGON")
          {
              screen.SendKeys("ISPF");
              screen.SendControlKey(IBMControlKeyCode.Transmit);
          }
          if (ScreenID3 == "OPTION")
          {
              screen.SendKeys("1");
              screen.SendControlKey(IBMControlKeyCode.Transmit);
          }
          if (ScreenID4 == "COMMAND")
          {
                     
              //Make sure the text is retrieved from the other screen
              while (string.IsNullOrEmpty(TextToCopy))
              {
                  System.Threading.Thread.Sleep(500);
              }
                     
              //Put in the text retrieved from the Open Systems program and reset the TextToCopy property
              screen.PutText("Profit", 6, 18);
              screen.PutText(TextToCopy, 6, 33);
      
              //Reset the TextToCopy property
              TextToCopy = string.Empty;
          }
      }
      

    To test this project

    1. If you are using an Open Systems terminal, open Reflection and create a new VT session. Then enter "demo:UNIX" in the IP/Address box and save the session as demoSession.rdox.
    2. In Visual Studio, press F5 to run the project.
    3. Verify that the two sessions open and that the text is retrieved from one session and put into the other.