Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 1998-09-28
Medium Priority
Last Modified: 2010-04-30
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.

Question by:ljaques

Expert Comment

ID: 1437139
'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
Dim TimeOutVal as Date
TimeOutVal = Now
Do Until FindWindow("", WindowCaption) <> 0
If DateDiff("s",TimeOutVal,Now) = > 2 then ShellWait = False: Exit Function'Time out if over 2 seconds pass
ShellWait = True
End Function
LVL 15

Expert Comment

ID: 1437140
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 :)

Expert Comment

ID: 1437141
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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 12

Expert Comment

ID: 1437142
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
    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
    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 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 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:
    ret& = WaitForSingleObject(proc.hProcess, 5000)
    If ret& <> 0 Then GoTo holdhere
ret& = CloseHandle(proc.hProcess)
End Sub

This will do what you want.



Author Comment

ID: 1437143
  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.

Author Comment

ID: 1437144
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.


Accepted Solution

sdbanks earned 200 total points
ID: 1437145
' 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.

LVL 12

Expert Comment

ID: 1437146
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.


Author Comment

ID: 1437147
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.


Author Comment

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

Thanks guys

Author Comment

ID: 1437149
There are certainly a lot of knowledgeable people around the world...Thanks to everyone who helped!

Expert Comment

ID: 1437150
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.

Expert Comment

ID: 5139286
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.

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question