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!!!
ljaquesAsked:
Who is Participating?
 
sdbanksConnect With a Mentor Commented:
***************************************************************************
' Declarations for the API calls
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) _
As Long

Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle _
As Long, ByVal dwProcessId As Long) As Long

Private Const INFINITE = -1&
Private Const SYNCHRONIZE = &H100000

' Run DOS executable or batch file when command key pressed
Private Sub Command1_Click()

' Define API return variables
Dim iTask As Long, ret As Long, pHandle As Long

' Start DOS command and retrieve handle of open process
iTask = Shell("MyCommand.exe", vbNormalFocus)
pHandle = OpenProcess(SYNCHRONIZE, False, iTask)

' Wait until DOS command has completed
ret = WaitForSingleObject(pHandle, INFINITE)
ret = CloseHandle(pHandle)

End Sub
**************************************************************************
If you change the shell status to VbHide then remember to also change the properties of the executable file to close on exit or
your program will hang.

0
 
AllenC_JrCommented:
'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
0
 
EyalCommented:
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 :)
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
NoggyCommented:
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.
0
 
mark2150Commented:
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

0
 
ljaquesAuthor Commented:
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.
0
 
ljaquesAuthor Commented:
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.

0
 
mark2150Commented:
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.

0
 
ljaquesAuthor Commented:
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.

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

Thanks guys
0
 
ljaquesAuthor Commented:
There are certainly a lot of knowledgeable people around the world...Thanks to everyone who helped!
0
 
davefocCommented:
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.
0
 
drewish_princessCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.