Solved

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

Posted on 1998-09-28
13
266 Views
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.

HELP!!!
0
Comment
Question by:ljaques
13 Comments
 
LVL 2

Expert Comment

by:AllenC_Jr
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
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
 
LVL 15

Expert Comment

by:Eyal
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 :)
0
 
LVL 4

Expert Comment

by:Noggy
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.
0
 
LVL 12

Expert Comment

by:mark2150
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
'
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
 

Author Comment

by:ljaques
ID: 1437143
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
 

Author Comment

by:ljaques
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.

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Accepted Solution

by:
sdbanks earned 50 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.

0
 
LVL 12

Expert Comment

by:mark2150
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.

0
 

Author Comment

by:ljaques
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.

0
 

Author Comment

by:ljaques
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
0
 

Author Comment

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

Expert Comment

by:davefoc
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.
0
 

Expert Comment

by:drewish_princess
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.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now