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 Command Help

Status
Not open for further replies.

bboggessswcg

Programmer
Jun 6, 2003
34
0
0
US
I need to be able to launch a batch file at the first load of my program and then wait for it to finish before continuing. I can launch the batch file just fine, but how do I make it wait? The batch could run up to 2 hours. Thanks in advance for your help.
 
The WaitForSingleObject API function can do this. Use the fShellWait function below instead of Shell.
Code:
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

Const NORMAL_PRIORITY_CLASS = &H20&
Const INFINITE = -1&

Declare Function WaitForSingleObject Lib "kernel32" (Byval _
hHandle As Long, Byval dwMilliseconds As Long) As Long

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

Declare Function GetExitCodeProcess Lib "kernel32" _
(Byval hProcess As Long, lpExitCode As Long) As Long
'______________________________________

Function fShellWait(strCmdLine As String) As Long

  '--- Shells the passed command line and waits for the process to finish
  '--- Returns the exit code of the shelled process

  Dim udtProc As PROCESS_INFORMATION, udtStart As STARTUPINFO
  Dim lngRtn As Long

  'Initialize the STARTUPINFO structure
  udtStart.cb = Len(udtStart)

  'Launch the shelled application and yield to it
   lngRtn = CreateProcessA(Clng(0), strCmdLine, Clng(0), Clng(0), Clng(1), _
   NORMAL_PRIORITY_CLASS, Clng(0), Clng(0), udtStart, udtProc)
   DoEvents

  'Wait for the shelled application to finish
  lngRtn = WaitForSingleObject(udtProc.hProcess, INFINITE)
  Call GetExitCodeProcess(udtProc.hProcess, lngRtn)
  Call CloseHandle(udtProc.hThread)
  Call CloseHandle(udtProc.hProcess)
  fShellWait = lngRtn

End Function
Paul Bent
Northwind IT Systems
 
This may not be the most glorious way to do this, but trap the AppID that SHELL returns if it successfully starts your batch file:

AppID = Shell(..., 1)

Then, use a looping process to do "AppActivate" until the task completes...

On Error resume next
AppActivate AppID
While Err.Number = 0' Task still running
DoEvents ' You could also set up a timer routine,
'but whatever you do here will take up CPU
'cycles anyway...
Wend
Err.Clear 'Error code will be set
On Error Goto 0 'be sure to reset the error checking

I'm sure that there must be a way via the API to check this, (since you have the appid) but I don't usually go there
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top