Code to wait until a separate testing program/application has completed before continuing

Posted on 2005-05-09
Last Modified: 2010-05-02
In VB control code I call a file/script of a different programming language - free ware program called AutoIt.  This file/script will open an application (i.e. Outlook) and test it.  What is the code to wait until this script has finished so the VB control pannel can carry on with the next stage of the program? I don't think I can pass paramaters into and out of the script.

I was thinking of getting the Process Image Name (Task Manager) of the application that has run and wait until it has finished/process name does not exist anymore (once the application is closed the script is finished) - before continuing with the VB code, for the next test.

Please amend the following code to make this possible:

'Call AutoIt Outlook Test
    Call oTest.OpenFile("C:\Development\AutoIt\Test3_MSOutlook.au3")
    Sleep 8000
    'Need code to check if OUTLOOK.EXE exists (Outlook process image name).  
    'If so assign it to PIName variable
    'If MS Outlook opened/OUTLOOK.EXE exists, wait until the test is over before proceeding.
    'Please amend this code if necessary
    If PIName = "OUTLOOK.EXE" Then
        'While  OUTLOOK.EXE/MS Outlook is still running
            'Wait until MS Outlook is closed and it is finished testing
        Sleep 2000
        oErrLog.LogError Pre, eTests.Test3, "MS Outlook", "MS Outlook failed to open", Err.Number
    End If
Question by:willwatters
    LVL 13

    Accepted Solution

    AutoIt can compile scripts into EXE files. Compile your script and then execute it using something like this

        Dim WshShell As Object
        Set WshShell = CreateObject("WScript.Shell")
        Me.Enabled = False
        WshShell.Run "C:\Development\AutoIt\Test3_MSOutlook.exe", , True
        Me.Enabled = True

    Now the program will stop while Test3_MSOutlook.exe is runing. I dont know if you can use this without compiling the script (using au3 extension) but try.
    LVL 38

    Expert Comment

    Here's a function that can test if outlook is running:

    Option Explicit

    Const TH32CS_SNAPPROCESS = &H2
    Const TH32CS_SNAPTHREAD = &H4
    Const TH32CS_SNAPMODULE = &H8
    Const TH32CS_INHERIT = &H80000000

    Private Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szExeFile As String * 260
    End Type
    Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)

    Private Function IsProcessRunning(strProcessModule As String) As Boolean

        Dim lngSnapShot As Long
        Dim ProcessEntry As PROCESSENTRY32
        Dim lngRet As Long
        Dim bRet As Boolean
        lngSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0)
        ProcessEntry.dwSize = Len(ProcessEntry)
        lngRet = Process32First(lngSnapShot, ProcessEntry)

        Do While lngRet > 0
            If InStr(1, ProcessEntry.szExeFile, strProcessModule, vbTextCompare) > 0 Then
                bRet = True
                Exit Do
            End If
            lngRet = Process32Next(lngSnapShot, ProcessEntry)
        'close our snapshot handle
        CloseHandle lngSnapShot
        IsProcessRunning = bRet
    End Function

    Private Sub Command1_Click()
        MsgBox IsProcessRunning("OUTLOOK.EXE")
    End Sub

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    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…

    728 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

    16 Experts available now in Live!

    Get 1:1 Help Now