VERSION 5.00
Begin VB.Form FormFirst 
   Caption         =   "FormFirst"
   ClientHeight    =   3096
   ClientLeft      =   336
   ClientTop       =   540
   ClientWidth     =   4680
   LinkTopic       =   "Form2"
   ScaleHeight     =   3096
   ScaleWidth      =   4680
   Begin VB.CheckBox CheckBegEndSession 
      Caption         =   "Use Begin/EndSession"
      Height          =   255
      Left            =   600
      TabIndex        =   1
      Top             =   360
      Width           =   3975
   End
   Begin VB.CommandButton CommandFirst 
      Caption         =   "Start 1st App"
      Height          =   855
      Left            =   1200
      TabIndex        =   0
      Top             =   960
      Width           =   1935
   End
End
Attribute VB_Name = "FormFirst"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'This application, together with SecondApp 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 CommandFirst_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 1st 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 "FirstApp's connection to Origin interrupted. " & _
                    "FirstApp will keep trying."
        End If
        
    Next i
    
    If Not SessionControl(False) Then Exit Sub

End Sub
