How can I wait until a program finishes ? 
Author Message
 How can I wait until a program finishes ?

The SHELL statment returns a Double value when executing a program. I think
this is some sort of program handle. Does anybody know how to check
(probably with this number) if the executed program has closed ?  (I know it
is possible!)

Help would be appreciated !
Pascal vd Heiden



Mon, 19 Aug 2002 03:00:00 GMT  
 How can I wait until a program finishes ?
The value that is returned is the taskID, useless for what you are trying.

Try using the CreateProcess api, or the FindWindow api.

Kirk



Quote:
> The SHELL statment returns a Double value when executing a program. I
think
> this is some sort of program handle. Does anybody know how to check
> (probably with this number) if the executed program has closed ?  (I know
it
> is possible!)

> Help would be appreciated !
> Pascal vd Heiden



Mon, 19 Aug 2002 03:00:00 GMT  
 How can I wait until a program finishes ?


Fri, 19 Jun 1992 00:00:00 GMT  
 How can I wait until a program finishes ?


Quote:
> The value that is returned is the taskID, useless for what you are trying.

> Try using the CreateProcess api, or the FindWindow api.

> Kirk

Thanks, but I cant get it working.

The following code i saw in a description for wait for DOS programs to
finish but it works only in VB3 or earlier and also this I cant get working
in VB6:

    Declare Function GetModuleUsage Lib "Kernel" (Byval hInst as Integer) as
Integer
--
    Dim hInst as Integer

    hInst = Shell(prog, 7)
    Do
        DoEvents
    Loop While GetModuleUsage(hInst) > 0



Mon, 19 Aug 2002 03:00:00 GMT  
 How can I wait until a program finishes ?
What you could probably do is use CreateProcess API and then use the
WaitForSingleObject API and wait in a loop for the process to be finished.

Ramesh



Quote:



> > The value that is returned is the taskID, useless for what you are
trying.

> > Try using the CreateProcess api, or the FindWindow api.

> > Kirk

> Thanks, but I cant get it working.

> The following code i saw in a description for wait for DOS programs to
> finish but it works only in VB3 or earlier and also this I cant get
working
> in VB6:

>     Declare Function GetModuleUsage Lib "Kernel" (Byval hInst as Integer)
as
> Integer
> --
>     Dim hInst as Integer

>     hInst = Shell(prog, 7)
>     Do
>         DoEvents
>     Loop While GetModuleUsage(hInst) > 0



Mon, 19 Aug 2002 03:00:00 GMT  
 How can I wait until a program finishes ?


Quote:
> What you could probably do is use CreateProcess API and then use the
> WaitForSingleObject API and wait in a loop for the process to be finished.

> Ramesh

But the CreatProcess API uses so many parameters, I dont know how they work.
Can somebody give me a description or sample code how to start a program
using CreateProcess ?

Thanks, Pascal



Tue, 20 Aug 2002 03:00:00 GMT  
 How can I wait until a program finishes ?


Fri, 19 Jun 1992 00:00:00 GMT  
 How can I wait until a program finishes ?
Pascal, Try the following;

Add a module to your project.

insert the following function definitions

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

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

in your main project insert the syntax
----------------------------------------------------------------------------
-
Private myprogram as String

Private Sub ShellW(ByVal strProg As String, ByVal lStyle As VbAppWinStyle)
Dim PId As Long
Dim PHandle As Long
Const Access As Long = &H100000

ProcessId = Shell(strProg, lStyle)
    Do
        PHandle = OpenProcess(Access, False, PId)
        If PHandle <> 0 Then
            CloseHandle PHandle
        End If
        DoEvents
    Loop Until PHandle = 0
End Sub

Private Sub cmdRun_Click()
    ShellW myprogram, vbMaximizedFocus
End Sub
----------------------------------------------------------------------------
--

Hope this helps.

        Mike.

Quote:

>The SHELL statment returns a Double value when executing a program. I think
>this is some sort of program handle. Does anybody know how to check
>(probably with this number) if the executed program has closed ?  (I know
it
>is possible!)

>Help would be appreciated !
>Pascal vd Heiden



Tue, 20 Aug 2002 03:00:00 GMT  
 How can I wait until a program finishes ?
You can place the following into a module.

When you call the "ExecuteAndWait" function pass one of the "SW_"  constants
to indicate how you want the program's window to appear when it  is
launched.

You might want to do a little reading on the "CreateProcess",
"WaitForSingleObject", and "CloseHandle" methods in order to add your own
error handling code to the function for those calls when unexpected values
are received in the local *lReturn* variable.

----------------------------------------------------------

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

Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type

Public Const SW_HIDE = 0
Public Const SW_SHOWNORMAL = 1
Public Const SW_NORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_MAXIMIZE = 3
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOW = 5
Public Const SW_MINIMIZE = 6
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE = 9
Public Const SW_SHOWDEFAULT = 10
Public Const SW_MAX = 10

Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&

Declare Function CloseHandle Lib "kernel32" (hObject As Long) As Boolean

Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long,
ByVal dwMilliseconds As Long) As Long

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

Global Const STARTF_USESHOWWINDOW = 1

Public Function ExecuteAndWait(sExecFileName As String, iShowCommand As
Integer) As Integer
    Dim NameOfProc As PROCESS_INFORMATION
    Dim NameStart As STARTUPINFO
    Dim lReturn As Long

    NameStart.cb = Len(NameStart)
    NameStart.dwFlags = STARTF_USESHOWWINDOW
    NameStart.wShowWindow = iShowCommand

    lReturn = CreateProcessA(0&, sExecFileName, 0&, 0&, 1&,
NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)

    lReturn = WaitForSingleObject(NameOfProc.hProcess, INFINITE)

    lReturn = CloseHandle(NameOfProc.hProcess)

    ExecuteAndWait = lReturn
End Function



Tue, 20 Aug 2002 03:00:00 GMT  
 How can I wait until a program finishes ?


Fri, 19 Jun 1992 00:00:00 GMT  
 How can I wait until a program finishes ?

Public Declare Function WaitForSingleObject Lib "kernel32" _
   (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

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

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

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

Public Sub shell_wait(program As String, window_style As Integer)
    Dim iTask As Long, ret As Long, pHandle As Long

    iTask = Shell(program, window_style)
    pHandle = OpenProcess(SYNCHRONIZE, False, iTask)
    Do
        ret = WaitForSingleObject(pHandle, 100)
        'Note I should really check for errors here
        If ret <> 0 Then
            DoEvents
        End If
    Loop Until ret = 0

    ret = CloseHandle(pHandle)
End Sub

Example on using this code would be:

shell_wait "c:\windows\notepad.exe", vbNormalFocus

Regards,
Michael.



Quote:



> > What you could probably do is use CreateProcess API and then use the
> > WaitForSingleObject API and wait in a loop for the process to be
finished.

> > Ramesh

> But the CreatProcess API uses so many parameters, I dont know how they
work.
> Can somebody give me a description or sample code how to start a program
> using CreateProcess ?

> Thanks, Pascal



Wed, 21 Aug 2002 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Calling an external program (and waiting until it's finished)

2. Waiting until external program finished to continue processing

3. Wait until document printing is finished

4. Wait until form_load is finished

5. Waiting until Shell finished

6. Wait until logonscript finishes executing...

7. Force a sub to wait until another sub (called first by a timer) is finished

8. Launching an App and Waiting Until it Finishes

9. halting VB program execution until OS finishes

10. Pause program execution until other application finishes

11. SHELL and wait until the end of the program

12. How to start program and wait until it is complete from VB 4.0

 

 
Powered by phpBB® Forum Software