Solved

Running applications

Posted on 1998-08-27
10
140 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now