Link to home
Start Free TrialLog in
Avatar of ljaques
ljaques

asked on

Any way to tell when a SHELL execute is DONE?!?

I used the shell command to call pkunzip a file on my HD.
However, i found out that the SHELL command is asyncronous meaning that it runs the code lines just below the shell command call.  If this occurs then my program will have problems.
Is there a way to tell when the SHELL's command call has finally finished (the pkunzip call is done)?  I know the SHELL command returns a task ID but i haven't found a way to use this in helping me tell when the call is done.

HELP!!!
Avatar of AllenC_Jr
AllenC_Jr

'Try this and if it doesn't work then reject it...

Private Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, ByVal lpWindowName$)

Private Function ShellWait(ExeName as String, WindowCaption) As Boolean
SHELL ExeName
Dim TimeOutVal as Date
TimeOutVal = Now
Do Until FindWindow("", WindowCaption) <> 0
DoEvents
If DateDiff("s",TimeOutVal,Now) = > 2 then ShellWait = False: Exit Function'Time out if over 2 seconds pass
Loop
ShellWait = True
End Function
when you use shell it returns an ID

use this ID in AppActivate
in the title put the ID you got from the shell function
and in the wait parameter put true

thats all my friends :)
I don't think that Eyal's comment will actually do the trick. Agreed, it will activate the app window but it will not stop the rest of your code from executing in the background.

AllenC_Jr's proposed answer is nearly correct. I actually tried it but needed to make the following modifications. Maybe it had something to do with me using Excel VBA rather than the full VB.

I noticed that the API library stated that lpClassName (in the Declare line) could be of any data type but I found that the string data type would not work. Instead, change the relevant lines to the following:

         Public Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName$)

         Do Until FindWindow(0, WindowCaption) = 0

You may also want to REM out the following lines to get the function to 'stall' your parent procedure's code until the app window is closed:

        If DateDiff("s", TimeOutVal, Now) >= 2 Then
            ShellWait = False
            Exit Function 'Time out if over 2 seconds pass
        End If

By the way, AllenC_Jr didn't quite specify what the WindowCaption was. If you're unsure, it's the name at the top of the window e.g. "Microsoft Excel - Book1". I should imagine yours will be "<Drive>:<Path>\PKUNZIP.EXE".

I hope this helps. Mind you, most of the credit should go to AllenC_Jr.
Here is the ExecCmd function from Microsoft that fires a child task and waits for it to complete before continuing. You set the properties of the .EXE file to Run Minimized and Close when Finished and it'll run essentially blind.

'
' These data type declarations are required to support the obscure windows
' function to spawn a child task and wait for it to finish before continuing
'
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&
'
'
' I don't really understand the above declares, but they do work so DON'T MESS WITH THEM!
'
Public Sub ExecCmd(cmdline$)
'
' This executes an external windows program and waits for it to complete
'
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:
'
holdhere:
    DoEvents
    ret& = WaitForSingleObject(proc.hProcess, 5000)
    If ret& <> 0 Then GoTo holdhere
'
ret& = CloseHandle(proc.hProcess)
End Sub

This will do what you want.

M

Avatar of ljaques

ASKER

mark2150:
  Your code works but is there a way to HIDE the MSDOS window and when it is done the SHELL task it returns back?

The SHELL command has a vbHide property to HIDE the MSDOS window but because this is something totally new to me i don't know how to do this with your code.
Avatar of ljaques

ASKER

I like MARK2150's proposal but is there a way to HIDE the MSDOS window?  I noticed the other proposals had the same problems as well...the SHELL function has a vbHide property BTW.

ASKER CERTIFIED SOLUTION
Avatar of sdbanks
sdbanks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The *COMMAND* doesn't provide a direct way to do this, but you can hide the window. Set the Properties of the PROGRAM that you're running to "Run Minimized" and "Close on Exit". The only visible indication of the child program will be a MS-DOS item in the taskbar that will go away when the child finishes execution.

Avatar of ljaques

ASKER

How do you set the programs properties so as to make it "Run Minimized" and "Close on Exit".  
If it helps I am calling "PKUNZIP c:\temp.zip" ...how do you tell PKUNZIP to have these properties?

Sorry for being so ignorant.

Avatar of ljaques

ASKER

oops...sorry, i figured out how to set the properties...I ALMOST NEVER go into the Properites feature of a file...

Thanks guys
Avatar of ljaques

ASKER

There are certainly a lot of knowledgeable people around the world...Thanks to everyone who helped!
i spent hours on this particular problem, tried the microsoft solution and another posted solution that worked on win95 but not on win NT.

looks to me like cleanest solution is the sdbanks solution.  I just tried it and it worked under win95. haven't tried it under winnt yet though.  since some of the processes that i start are very long i like to monitor progress so i put the WaitForSingleObject call in a timer proc and check on progress every 100 ms (by checking file size).  I didn't do it in line because i was afraid VB might use too many cycles for continuous polling and slow down the process i just started too much.
one way to run an batch file with out having to change the properties of command.com is to call "command.com /c myscript.bat" this will create a command window that runs the batch file then exits.