Results 1 to 4 of 4

Thread: Stopping execution until after a shelled program has been run

  1. #1

    Thread Starter
    Fanatic Member Kzin's Avatar
    Join Date
    Dec 2000
    Posts
    611

    Question

    Does anyone know of a way of stopping execution of a VB6 program until after a shelled program has been run?

    So that if I have something like

    Shell "C:\WINDOWS\Desktop\tmp.exe"
    Kill "C:\WINDOWS\Desktop\tmp.exe"


    I can wait until tmp.exe has stopped running to get rid of it?

  2. #2
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186
    Start the program using Shell. Wait for it using WaitForSingleObject.
    Code:
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    Private Const SYNCHRONIZE = &H100000
    Private Const INFINITE = -1&
    
    ' Start the indicated program and wait for it
    ' to finish, hiding while we wait.
    Private Sub ShellAndWait(ByVal program_name As String)
    Dim process_id As Long
    Dim process_handle As Long
    
        ' Start the program.
        On Error GoTo ShellError
        process_id = Shell(program_name, vbNormalFocus)
        On Error GoTo 0
    
        ' Hide.
        Me.Visible = False
        DoEvents
    
        ' Wait for the program to finish.
        ' Get the process handle.
        process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
        If process_handle <> 0 Then
            WaitForSingleObject process_handle, INFINITE
            CloseHandle process_handle
        End If
    
        ' Reappear.
        Me.Visible = True
        Exit Sub
    
    ShellError:
        MsgBox "Error starting task " & _
            txtProgram.Text & vbCrLf & _
            Err.Description
    End Sub
    Hope this helps.
    Shrog
    VB6 Ent SP5
    Win2000

  3. #3
    Fanatic Member faisalkm's Avatar
    Join Date
    Oct 2000
    Location
    Germany
    Posts
    752
    Const STILL_ACTIVE = &H103
    Const PROCESS_QUERY_INFORMATION = &H400
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

    Public Function gRunProgrammAndWait(sJobToDo As String) As Boolean
    Dim hProcess As Long
    Dim lRetVal As Long
    On Error GoTo err_gRunProgrammAndWait
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(sJobToDo, 0))
    Do
    GetExitCodeProcess hProcess, lRetVal
    DoEvents
    Loop While lRetVal = STILL_ACTIVE
    gRunProgrammAndWait = True
    Exit Function
    err_gRunProgrammAndWait:
    gRunProgrammAndWait = False
    Exit Function
    End Function


    Now u can call like this

    Private Command1_Click()
    if gRunProgramandWait(yourshellcommand) =True then
    Msgbox "Task Finished"
    end if
    end sub
    Faisal Muhammed
    Homepage:I Started making it in 1994 ...Still Under Construction
    Using

    Visual Basic 6.0 Enterprise SP5
    Embedded Visual Basic 3.0
    SQL Server 2000
    Windows 2000 Proff
    Delphi 6.0


    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  4. #4

    Thread Starter
    Fanatic Member Kzin's Avatar
    Join Date
    Dec 2000
    Posts
    611

    Thumbs up

    Thanks Shrog & faisalkm - greatly appreciated!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width