Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell problems 1

Status
Not open for further replies.

pds

Programmer
Jul 2, 2001
9
US
Hi, I've got a VB5 program that has a number of Shell functions that run a DOS program. Originally, I had problems with it because, as you probably know, the statements following the shell function are executed whether the shell function completed its calculations or not. As a result, I created a random number generator that gave me crazy results.

Then I threw in a timer function (thanks to whoever suggested that) that paused the program for 1 second after the shell function, allowing the DOS program to actually finish. Now I am actually getting constant answers, but I have to wait some 5 minutes or even more for the entire program to compute.

Question: Is there some sort of statement that I can put after a shell function that waits for and recognizes when the shell finishes , and then continues on with the program?
Thanks.
 
Code:
Call ShellAndWait(strProgram, vbMaximized)
........................

'==== bas module - declarations section ========
Option Explicit

Private Declare Function OpenProcess Lib "kernel32" _
       (ByVal dwDesiredAccess As Long, _
        ByVal bInheritHandle As Long, _
        ByVal dwProcessId As Long) As Long
Private Declare Function WaitForInputIdle Lib "user32" _
       (ByVal hProcess As Long, +
        ByVal dwMilliseconds 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 INFINITE = &HFFFF
Private Const SYNCHRONIZE = &H100000

'==== bas modue - code section ====
Public Sub ShellAndWait(sAppPath As String, Optional _
                        iWindowStyle As VbAppWinStyle = vbMinimizedFocus, _
    Optional  lmsTimeOut As Long = INFINITE)
    'Optional argument lmsTimeOut specifies milliseconds to wait.
   'Wait forever if omitted (default).
    Dim lPid As Long, hProc As Long, lTimeOut As Long

    lPid = Shell(sAppPath, iWindowStyle)
    hProc = OpenProcess(SYNCHRONIZE, 0&, lPid)
    If hProc <> 0& Then
        WaitForInputIdle hProc, INFINITE
        WaitForSingleObject hProc, lmsTimeOut
        CloseHandle hProc
    End If
End Sub
 
JohnYingling:

I've been trying to use your code. I put it into a bas module that's in the same project group as the form I'm using, and I've typed in the command just as your code says:

---------------------------------------
Call ShellAndWait(&quot;C:\foo\bar.exe&quot;, 0)
---------------------------------------

But I receive the error message, &quot;Compile error: Expected variable or procedure, not module&quot;.

Any help would be greatly appreciated.

pds
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top