Solved

Running applications

Posted on 1998-08-27
10
142 Views
Last Modified: 2010-04-30
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
Comment
Question by:lafronte
  • 5
  • 4
10 Comments
 
LVL 3

Accepted Solution

by:
dapperry earned 100 total points
ID: 1431393
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
 
LVL 3

Expert Comment

by:dapperry
ID: 1431394
Just to clarify.  So call your applications by using the ExecCmd function, instead of using Shell.

:) David
0
 
LVL 2

Expert Comment

by:dabellei
ID: 1431395
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

Author Comment

by:lafronte
ID: 1431396
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
 
LVL 3

Expert Comment

by:dapperry
ID: 1431397
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
 

Author Comment

by:lafronte
ID: 1431398
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
 

Author Comment

by:lafronte
ID: 1431399
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
 
LVL 3

Expert Comment

by:dapperry
ID: 1431400
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
 
LVL 3

Expert Comment

by:dapperry
ID: 1431401
Also, is the app you are running 32-bit?

:) D Perry
0
 

Author Comment

by:lafronte
ID: 1431402
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question