Solved

Visual Basic Question Open Applications and wait until they finish in Windows 2000

Posted on 2001-09-07
8
251 Views
Last Modified: 2013-11-25
Hi,

I am new to programming. I am looking for a sub routine to lauch an application (which inturn lauches application, which inturn lauches another application again).And this routine has to wait until the first application or all applications to finish to take some action depending on the result. I need a solution as soon as possible. Please help me out.

Thanks
Bhaskar
0
Comment
Question by:bhaskara_rkk
8 Comments
 
LVL 5

Expert Comment

by:rkot2000
ID: 6464278
different shell options :


you can replace Shelled1.exe with notepad.exe
VERSION 4.00
Begin VB.Form frmLaunch
   Caption         =   "Launch Shelled1.exe"
   ClientHeight    =   2880
   ClientLeft      =   1095
   ClientTop       =   1515
   ClientWidth     =   4140
   Height          =   3285
   Left            =   1035
   LinkTopic       =   "Form1"
   ScaleHeight     =   2880
   ScaleWidth      =   4140
   Top             =   1170
   Width           =   4260
   Begin VB.CommandButton cmdShell2
      Caption         =   "Shell with Callback"
      Height          =   495
      Left            =   240
      TabIndex        =   6
      Top             =   1560
      Width           =   1755
   End
   Begin VB.CommandButton cmdShellExecute
      Caption         =   "Using ShellExecute"
      Height          =   495
      Left            =   240
      TabIndex        =   3
      Top             =   2220
      Width           =   1755
   End
   Begin VB.CommandButton cmdCreateProcess
      Caption         =   "Using CreateProcess"
      Height          =   495
      Left            =   240
      TabIndex        =   2
      Top             =   900
      Width           =   1755
   End
   Begin VB.CommandButton cmdShell
      Caption         =   "Using Shell"
      Height          =   495
      Left            =   240
      TabIndex        =   0
      Top             =   240
      Width           =   1755
   End
   Begin VB.Label lblStatus
      Height          =   255
      Index           =   2
      Left            =   2100
      TabIndex        =   5
      Top             =   1680
      Width           =   1875
   End
   Begin VB.Label lblStatus
      Height          =   255
      Index           =   1
      Left            =   2100
      TabIndex        =   4
      Top             =   1020
      Width           =   1875
   End
   Begin VB.Label lblStatus
      Height          =   255
      Index           =   0
      Left            =   2100
      TabIndex        =   1
      Top             =   300
      Width           =   1875
   End
End
Attribute VB_Name = "frmLaunch"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Option Explicit
' Copyright ? 1997 by Desaware Inc. All Rights Reserved

Dim DemoDirectory$
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFF           '  Infinite timeout
Private Const DEBUG_PROCESS = &H1
Private Const DEBUG_ONLY_THIS_PROCESS = &H2

Private Const CREATE_SUSPENDED = &H4

Private Const DETACHED_PROCESS = &H8

Private Const CREATE_NEW_CONSOLE = &H10

Private Const NORMAL_PRIORITY_CLASS = &H20
Private Const IDLE_PRIORITY_CLASS = &H40
Private Const HIGH_PRIORITY_CLASS = &H80
Private Const REALTIME_PRIORITY_CLASS = &H100

Private Const CREATE_NEW_PROCESS_GROUP = &H200

Private Const CREATE_NO_WINDOW = &H8000000

Private Const WAIT_FAILED = -1&
Private Const WAIT_OBJECT_0 = 0
Private Const WAIT_ABANDONED = &H80&
Private Const WAIT_ABANDONED_0 = &H80&
Private Const WAIT_TIMEOUT = &H102&

Private Const SW_SHOW = 5

Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
End Type

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 Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessBynum Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDirectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long



Private Sub cmdCreateProcess_Click()
    Dim res&
    Dim sinfo As STARTUPINFO
    Dim pinfo As PROCESS_INFORMATION
    sinfo.cb = Len(sinfo)
    sinfo.lpReserved = vbNullString
    sinfo.lpDesktop = vbNullString
    sinfo.lpTitle = vbNullString
    sinfo.dwFlags = 0
   
    lblStatus(1).Caption = "Launching"
    lblStatus(1).Refresh
    res = CreateProcessBynum(DemoDirectory & "Shelled1.exe", vbNullString, 0, 0, True, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, sinfo, pinfo)
    If res Then
        lblStatus(1).Caption = "Launched"
        WaitForTerm2 pinfo
    End If
    lblStatus(1).Caption = "Terminated"

End Sub

Private Sub cmdShell_Click()
    Dim pid&
    lblStatus(0).Caption = "Launching"
    lblStatus(0).Refresh
    pid = Shell(DemoDirectory & "Shelled1.exe", vbNormalFocus)
    If pid <> 0 Then
        lblStatus(0).Caption = "Launched"
        lblStatus(0).Refresh
        WaitForTerm1 pid
    End If
    lblStatus(0).Caption = "Terminated"
End Sub

' This wait routine freezes the application
' It's clearly not a good way to wait for process
' termination - though if you hid the application
' first it could be very effective.
Private Sub WaitForTerm1(pid&)
    Dim phnd&
    phnd = OpenProcess(SYNCHRONIZE, 0, pid)
    If phnd <> 0 Then
        lblStatus(0).Caption = "Waiting for termination"
        lblStatus(0).Refresh
        Call WaitForSingleObject(phnd, INFINITE)
        Call CloseHandle(phnd)
    End If
End Sub

' This wait routine allows other application events
' to be processed while waiting for the process to
' complete.
Private Sub WaitForTerm2(pinfo As PROCESS_INFORMATION)
    Dim res&
    ' Let the process initialize
    Call WaitForInputIdle(pinfo.hProcess, INFINITE)
    ' We don't need the thread handle
    Call CloseHandle(pinfo.hThread)
    ' Disable the button to prevent reentrancy
    cmdCreateProcess.Enabled = False
    lblStatus(1).Caption = "Waiting for termination"
    lblStatus(1).Refresh
    Do
        res = WaitForSingleObject(pinfo.hProcess, 0)
        If res <> WAIT_TIMEOUT Then
            ' No timeout, app is terminated
            Exit Do
        End If
        DoEvents
    Loop While True
   
    cmdCreateProcess.Enabled = True
    ' Kill the last handle of the process
    Call CloseHandle(pinfo.hProcess)
End Sub

Private Sub cmdShell2_Click()
    Dim pid&
    Dim obj As Object
    lblStatus(2).Caption = "Launching"
    lblStatus(2).Refresh
    pid = Shell(DemoDirectory & "Shelled1.exe", vbNormalFocus)
    If pid <> 0 Then
        Set obj = CreateObject("dwWatcher.dwAppWatch")
        obj.SetAppWatch pid
        obj.SetAppCallback Me
        lblStatus(2).Caption = "Waiting for termination"
        cmdShell2.Enabled = False
    End If
End Sub

Private Sub cmdShellExecute_Click()
    Dim res&
    Dim obj As Object
    res& = ShellExecute(hwnd, "open", DemoDirectory & "Shelled1.exe", vbNullString, CurDir$, SW_SHOW)
    If res < 32 Then
        MsgBox "Unable to shell applicatin"
    End If
End Sub

Public Sub dwAppTerminated(obj As Object)
    lblStatus(2).Caption = "Terminated"
    cmdShell2.Enabled = True
End Sub

Private Sub Form_Load()
    DemoDirectory = InputBox$("Enter path of directory containing Shelled1.exe", , "d:\zdbook3\source\ch14\")
End Sub

0
 

Expert Comment

by:luiggie
ID: 6464286
This code wait until the exe program finish and the continue with the next line of execution.

'Use this API's and constants

Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal  hProcess As Long, lpExitCode As Long) As
Long

Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)

Global Const STILL_ACTIVE = &H103
Global Const PROCESS_QUERY_INFORMATION = &H400

Dim a as long
dim str as string
str = "your exe program path"
a = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(str, vbHide))
Do
   'Get the status of the process
   GetExitCodeProcess a, RetVal
   'Sleep command recommended as well as DoEvents
   DoEvents: Sleep 100
   'Loop while the process is active
Loop While RetVal = STILL_ACTIVE

Hope this help you.
Luis M. Ramos
IT Consultant, Metro IS
www.metrois.com
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6465103
hearing...
0
 
LVL 3

Expert Comment

by:adg
ID: 6467671
ping
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:glass_cookie
ID: 6471166
Hi!

Here's a file for you over the net:

Download...
http://www.planetsourcecode.com/vb/scripts/ShowZip.asp?lngWId=1&lngCodeId=25299&strZipAccessCode=hell252991772

Description: This module allow you to launch a program, wait until it ends and read the console output. Unlike others code that i have seen, this one don't hang your program while it is waiting. You can also set a timeout delay and cancel the execution at any time. N.B: You should use it only with DOS or console program (Netstat, TraceRc, etc...)

View code...
http://www.planetsourcecode.com/vb/scripts/ShowCodeAsText.asp?txtCodeId=3716&lngWId=1

Description: Unlike the Shell command in VB which launches an application asynchronous, this will launch the program synchronous. What that means is that the shell execute command will launch an application but not wait for it to execute before processing the next line of code. This code will launch a program then wait until the executable has terminated before executing the next line of code.


That's it!

glass cookie : )

PS. Happy programming : )
0
 
LVL 27

Accepted Solution

by:
Ark earned 300 total points
ID: 6533700
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "C:\MyCollApp.Exe param1", 1, True
Set WshShell = Nothing

Cheers
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6802555
There has been no activity in this question in quite some time, and it looks like it has been abandoned. As part of our ongoing mission to clean up the topic areas, a Moderator will finalize this question within the next seven (7) days. Experts, please leave your suggestions for an equitable resolution to this question.

DO NOT ACCEPT THIS COMMENT AS AN ANSWER. If you have further comments on this question or the recommendation, please leave them here.

Thanks,

Netminder
Community Support Moderator
Experts Exchange
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6820648
Simple and to the point. Force/accepted by

Netminder
Community Support Moderator
Experts Exchange
 
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

708 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

13 Experts available now in Live!

Get 1:1 Help Now