Call an Exe and wait until it is executed

Posted on 2003-02-28
Medium Priority
Last Modified: 2010-08-05
I have written a small program/EXE in .NET (which does some image preprocessing).  What I need is, I want to call this from my VB 6.0 application (which does a lot of other things).  If I call the EXE using the "Shell" function, it will run the EXE; but it will not wait till the execution is finished.  It will go to the next statement.

Is there any other way to call an EXE which will wait until the execution of the EXE is over and then go to the next statement ?
Question by:benvarg
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 70

Accepted Solution

Éric Moreau earned 80 total points
ID: 8042243
You can use this:

Option Explicit

Private Declare Function WaitForSingleObject _
                 Lib "kernel32" _
                 (ByVal hHandle As Long, _
                 ByVal dwMilliseconds 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 = &HFFFF

Private Sub Command1_Click()
    WaitForProcessToEnd "CALC.EXE"
End Sub

Private Function WaitForProcessToEnd(ByVal pstrExecute As String) As Boolean
'*** Execute a process an wait for it to end before continuing here ***
Dim lngHandle As Long
Dim lngProcessID As Long
Dim lngReturnValue As Long

    On Error GoTo ErrorHandler
    WaitForProcessToEnd = False
    lngProcessID = Shell(pstrExecute)
    lngHandle = OpenProcess(&H100000, True, lngProcessID)
    lngReturnValue = WaitForSingleObject(lngHandle, INFINITE)
    MsgBox "Here is the proof that I was waiting!!!"
    WaitForProcessToEnd = True
    Exit Function
    Select Case Err.Number
        Case 53  'File not found
            MsgBox pstrExecute & " cannot be found.", vbOKOnly + vbCritical
            Resume GoHere
        Case Else
            MsgBox Err.Number & " : " & Err.Description
            Resume GoHere
    End Select
End Function


Author Comment

ID: 8042340
Hi emoreau,

That is good.  I just tried it as it is.  Now let me try with my actual application.


Author Comment

ID: 8047435
Thanks emoreau.  You gave me the complete code.  It is what I wanted.  Thank You

Featured Post

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!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, left to enroll

770 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