Link to home
Start Free TrialLog in
Avatar of liljegren
liljegrenFlag for Sweden

asked on

Knowing when a Shell task is finished?

I'm trying to find a reliable way to know when a Shell task has finished.

From the KB article # Q127030:

"The Shell function in Microsoft Visual Basic is used to execute an application. Often, it is useful to get a Window handle (hWnd) to the application so you can manipulate it using the Windows APIs. Unfortunately, the Shell function returns an Instance handle (hInstance), which is different from a Window handle. This article shows by example how to use the GetWinHandle() function to return a Window handle based on an Instance handle."

Is this transformation necessary for what I'm doing? Do I need to transform it to a Windows handle to know when the Shell task has finished?

Specifically, what I do is to use two command line programs sequentially -- the output of the first program is the input of the other. The problem is that VB just keeps on running, so the second Shell function starts before the file is ready!

I was previously recommended using the CreateProcess API function instead of Shell. I remember I tried and I remember I wasn't successful, but I don't remember why. I hope transforming the Instance handle to a Windows handle and wait for the handle to terminate is an alternative solution. What do you think?

The most convincing suggestion wins. :-)
Avatar of Erick37
Erick37
Flag of United States of America image

See this example:

"Wait for a Process to Terminate"
http://www.thescarms.com/vbasic/wait.asp
Another example from MS:

"HOWTO: 32-Bit App Can Determine When a Shelled Process Ends (Q129796)"
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q129796&GSSNB=1
Avatar of johnwood
johnwood

Listioning......
The above suggestions are about "How to sleep and wait a process to terminate".
Any help for " When I need to know, how to check if a process is finished"?
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

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
Avatar of glass_cookie
Hi!

Here's a file for you over the ent:

Download...
http://www.vb-helper.com/HowTo/runwait.zip

Description: Start another program and wait until it finishes (2K)

That's it!

glass cookie : )
Dim lPid As Long
Dim lHnd As Long
Dim lRet As Long

If Trim$(txtApp) = "" Then Exit Sub

lPid = Shell(txtApp.Text, vbNormalFocus)
If lPid <> 0 Then
        'Get a handle to the shelled process.
        lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
        'If successful, wait for the application to end and close the handle.
        If lHnd <> 0 Then                                                      
                lRet = WaitForSingleObject(lHnd, INFINITE)
                CloseHandle (lHnd)
        End If
        MsgBox "Just terminated.", vbInformation, "Shelled Application"
End If


This code component is available at: http://www.thescarms.com/vbasic/wait.asp

Avatar of liljegren

ASKER

Yes, it works! But I got a new problem, which will be the material for next question.