Solved

Running applications

Posted on 1998-08-27
10
139 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

746 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