• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 148
  • Last Modified:

Running applications

I need to run two applications.  But I need to wait for the first one to finish before I start the second one.  I'm using the Shell function to run both apps.  Is there another function that I can use to run an app and have it finish before the other starts?  
0
lafronte
Asked:
lafronte
  • 5
  • 4
1 Solution
 
dapperryCommented:
Yes there is.  You can use an API call to spawn the process.  I have a little routine here that does it.  It waits for the program to finish executing, before proceeding.  I think this should help.  By the way, I heard that in VB 6, that they will have an argument to the shell command, to do just this, but I'm not sure if thats just rumor.

Put this code in a new module:

'This module hosts the ExecCmd
'function, which allows you to
'execute a program, and then wait
'until the process is done, before
'returning to the calling procedure.
'

Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
    hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
    lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
         lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
         ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
         ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
         lpStartupInfo As STARTUPINFO, lpProcessInformation As _
         PROCESS_INFORMATION) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal _
    hObject As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
      Private Const INFINITE = -1&

Public Sub ExecCmd(cmdline$)
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO

    ' Initialize the STARTUPINFO structure:
    start.cb = Len(start)

    ' Start the shelled application:
    ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
    NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

    ' Wait for the shelled application to finish:
    ret& = WaitForSingleObject(proc.hProcess, INFINITE)
    ret& = CloseHandle(proc.hProcess)
End Sub


Then just call the ExecCmd function from anywhere in your project.
Let me know if you have any questions.

:) D Perry
0
 
dapperryCommented:
Just to clarify.  So call your applications by using the ExecCmd function, instead of using Shell.

:) David
0
 
dabelleiCommented:
Sub commercial_apps(apps As String)
   
      Dim proc As PROCESS_INFORMATION
      Dim Start As STARTUPINFO
      Start.wShowWindow = False

      ' Initialize the STARTUPINFO structure:
      Start.cb = Len(Start)

      ' Start the shelled application:
          ret& = CreateProcessA(0&, apps, 0&, 0&, 1&, _
             NORMAL_PRIORITY_CLASS, 0&, 0&, Start, proc)
      End If
      ' Wait for the shelled application to finish:
      ret& = WaitForSingleObject(proc.hProcess, INFINITE)
      ret& = CloseHandle(proc.hProcess)
   
   
   
End Sub


call commercial_apps("application name and path")

This code is working very well i use it in my own programm..
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
lafronteAuthor Commented:
I had not tried the code until recently but the problem I'm running into now is that it will wait for a period of time but not long enough.  The orginal application is still running when the second one is launched.  Should I change the INIFINITE?
0
 
dapperryCommented:
Two questions - 1) What OS 2) What are you tring to run?  I've noticed that running control panel  applets do seem to time out waiting, but works for everything else.  Let me know and I'll see if I can help you out.

:) D Perry
0
 
lafronteAuthor Commented:
I had not tried the code until recently but the problem I'm running into now is that it will wait for a period of time but not long enough.  The orginal application is still running when the second one is launched.  Should I change the INIFINITE?
0
 
lafronteAuthor Commented:
The OS that I'm running is Win95 OSR2.  And my app is running the Share Agent for Oracle.  What happens is my app will launch the share agent and wait but when the share agent starts installing tools it exits the routine, while the share agent is still open, and continues through my code.  This will then launch Oracle Applications before it has been updated.  
0
 
dapperryCommented:
Do you know if the Agent (the intial process) spawns off another process, and then closes itself, eg You are trying to launch a process that end up launching another process.  If so, we could have problems because the ExecCmd waits for 1 particular process to end.  Let me know if this is case.

:) D Perry
0
 
dapperryCommented:
Also, is the app you are running 32-bit?

:) D Perry
0
 
lafronteAuthor Commented:
I think Oracle Installer does launch another process but initial process was never closed.  I'm pretty sure its a 16-bit app because it installs the 16 bit version of Oracle Applications.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now