VERSION 5.00
Begin VB.Form FormSecond 
   Caption         =   "FormSecond"
   ClientHeight    =   3096
   ClientLeft      =   336
   ClientTop       =   4320
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3096
   ScaleWidth      =   4680
   Begin VB.CheckBox CheckBegEndSession 
      Caption         =   "Use Begin/EndSession"
      Height          =   255
      Left            =   480
      TabIndex        =   1
      Top             =   360
      Width           =   3975
   End
   Begin VB.CommandButton CommandSecond 
      Caption         =   "Start 2nd App"
      Height          =   855
      Left            =   1440
      TabIndex        =   0
      Top             =   960
      Width           =   1935
   End
End
Attribute VB_Name = "FormSecond"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'This application, together with FirstApp will try to share the space of
'Origin Script Window, as well as Origin Automation Server
'Try running them both with different options
'
'Copyright OriginLab Corporation 2003.
'
'make sure Origin reference is checked (VB menu Project->References...)
Dim obj As Origin.ApplicationSI

Private Function SessionControl(bStart As Boolean) As Boolean
    'use this function to reserve or release exclusive session with Origin
    SessionControl = False
    On Error Resume Next
    
    If CheckBegEndSession.Value = vbChecked Then
        'Invoke Origin Automation method to Begin/End the session
        If bStart Then
            SessionControl = obj.BeginSession()
        Else
            SessionControl = obj.EndSession()
        End If
    Else
        'Nothing to do
        SessionControl = True
    End If
    
    If Not SessionControl Then
        MsgBox "SessionControl is unable to handle connection to Origin." & _
                "You mey try again later"
    End If
End Function

Private Sub CommandSecond_Click()
    Dim strCommand As String
    
    'Make a connection to Origin
    Set obj = GetObject("", "Origin.ApplicationSI")
    
    'If check box is on, the following function will try to
    'reserve exclusive control over Origin session
    If Not SessionControl(True) Then Exit Sub
    
    For i = 1 To 500
        strCommand = "type -a 2nd APP " & i
        If CheckBegEndSession.Value = vbChecked Then
            strCommand = strCommand & " USING Begin/End Session"
        End If
        
        'just type some text in Origin Script Window
        'to see if we (still) have the connection.
        On Error Resume Next
        bb = False
        bb = obj.Execute(strCommand)
        If Not bb Then
            MsgBox "SecondApp's connection to Origin interrupted. " & _
                    "SecondApp will keep trying."
        End If
        
    Next i
    
    If Not SessionControl(False) Then Exit Sub

End Sub
