Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell Help Needed

Status
Not open for further replies.

dirksm

Programmer
Mar 14, 2002
22
0
0
GB
Hi All.

I'm using the Shell function to run to batch programs. The first batch file starts a database manager and the second batch file creates tables in the database. These two commands are part of one "Create Database" on_click event. Both these commands work perfectly on their own.

My problem is that the firt command MUST complete before the second command is executed. The shell command runs asynchronously, which means that sometimes the second program starts running before the first has completed - causing all kinds of havoc!!

Does anybody know of a way to check if the first program is still running, and only start the second program once the first has completed?

Maybe some kind of Do-While loop?
 
I solved this problems using the API commands of windows: CreateProcess to start the program execution, WaitForSingleObject to wait until it is finished and CloseHandle to close it.

I don't known another way to wait for a dos application on VB.

If you are interested here is a sample of the code I used for:

Sub DosExecute(fileName as string)

Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
Dim MyCommandLine As String

'' Use the /C parameter after command.com to automatically close the dos windows at the end of the program execution
'' (CMD.EXE /C on windows NT)

'' MyCommandLine = fileName
MyCommandLine = "COMMAND.COM /C " & fileName

' standard assignment of start.cb to use CreateProcess
start.cb = Len(start)

ret = CreateProcess(vbNullString, MyCommandLine$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

' wait to the end of the program execution
While ret <> WAIT_OBJECT_0
ret = WaitForSingleObject(proc.hProcess, 1)
DoEvents
Wend

' execution finished
ret = CloseHandle(proc.hProcess)

End Sub

Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&
Public Const WAIT_OBJECT_0& = 0


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

Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type

Public Declare Function CreateProcess Lib &quot;kernel32&quot; Alias &quot;CreateProcessA&quot; _
(ByVal lpApplicationName As String, _
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

Public Declare Function WaitForSingleObject Lib &quot;kernel32&quot; _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Public Declare Function CloseHandle Lib &quot;kernel32&quot; (ByVal hObject As Long) As Long

Public Declare Function GetLastError Lib &quot;kernel32&quot; () As Long
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top