We help IT Professionals succeed at work.

running programs consecutively

lance_keen
lance_keen asked
on
Medium Priority
216 Views
Last Modified: 2012-06-21
Is there a way to execute a number of programs (I'm from the old Wang world where we used procedures for this, as
run program 1
run program 2
etc
Comment
Watch Question

CERTIFIED EXPERT
Commented:
If you mean from your VB program try using this. If you don't want to wait for the program to finish just get rid of the wait code:

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&



'In your code:

Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO

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

'Start the shelled application:
ret& = CreateProcessA(0&, "Program1.exe", 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)




Commented:
Use the Shell Function to execute a program external to yours.
Shell "C:\Program1.exe"

RH.
Richie_SimonettiIT Operations
CERTIFIED EXPERT

Commented:
If you need to wait from program1 to finish and launch program2, use hes code.
If it doesn't matter, use RHuebner one.
Cheers

Commented:
type in 'Do Events'
this command will allow you to run two seperate segments of code at the same time using equal amounts of the processor.
hes
CERTIFIED EXPERT

Commented:
LOL Do Events
That should be DoEvents no space, and that does not allow 2 seperate segments of code to run at the same time LOL
Just to clarify a bit on hes last comment :)

DoEvents interrupts the current code to allow any outstanding events to be processes, like mouse click and the like. Once those events have been processed, the execution continues from the line after the doevents. So there is no concurrent execution or anything like that happening, it is just a way for the current code to pause to let others have a chance :)

Zaphod.
Ark
CERTIFIED EXPERT

Commented:
Hi
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "notepad.exe", 1, TRUE
WshShell.Run "calc.exe", 1, TRUE
Set WshShell = Nothing

Cheers
Ark
CERTIFIED EXPERT

Commented:
PS. If you want run programs asynchroniously, change last parameter(WaitOnReturn) to FALSE.

Two comments:

1. If you want them to run concurrently, use shell, it is way more efficient and you don't lose anything.
2. If you want them to run sequentially, and you use Ark's method, be careful. I believe, although I am not sure, that that method requires windows scripting host to be installed on the machine that it is run on. Windows scripting host is a part of windows 2000, but it is a separate install on previous versions on windows. Also, again I am not sure, but I believe that VB will not catch this dependency if you use the package and deployment wizard because the createobject method is being used. Also, this method is much less efficient than hes' method, which may or may not be a problem for you.

Zaphod.
Ark
CERTIFIED EXPERT

Commented:
>>Windows Script Host is integrated into Windows 98, Windows 2000 Professional, and Windows 2000 Server. Version 2.0 includes the VBScript and JScript scripting engines.<<
(C)2000, Microsoft Corporation

Also, it integrated into IE4.01 and up.

Cheers

Ark
CERTIFIED EXPERT

Commented:
PS. I'm not voting for using wscript, just show another way then API.

BTW, there is one more way:

Open App.Path + "\Dummy.bat" For Append As #1
Print #1, "@echo off"
Print #1, "Programm1 Param1"
Print #1, "Programm2 Param2"
Print #1, "del " + App.Path + "\Dummy.bat"
Close #1
'--------------------------------
Shell App.Path + "\Dummy.bat", vbHide

Cheers
If this proposed answer has helped you, please accept it, grade it and close this question.  If it did not, reject it and add comments as to your progress and what else is needed.  If no action is taken here by next week, we'll need to decide this outcome.  Expert input always welcome as well.

Thanks,
Moondancer
Community Support Moderator @ Experts Exchange
Zero response to my request.  Proposed Answer rejected, another accepted/item closed.
Moondancer - EE Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.