Re: How to tell the end of a shell
- From: Jason Keats <jkeats@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 10 May 2008 15:49:09 +1000
inquirer wrote:
I have a VB6 program which runs shell which runs another program which in turn creates a file. As soon as I call the shell, I run a subroutine which essentially pauses the main program for a few seconds whilst the new file is created. I then use VB to open Word and read the newly created file.
This works fine when running from my hard disk but when I try to run it from a USB memory stick, the shell program takes several minutes to create the file.
I need to be able to pause the main program until the shell program has finished so I can then open the new file in Word.
Is there any way I can find out when the shell program has finished creating the new file?
Thanks
Chris
Do a search on ShellAndWait, or use something like the following...
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
' Shell & Wait...
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
'Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const STATUS_PENDING = &H103&
Private Const PROCESS_QUERY_INFORMATION = &H400
Public Function ShellAndWait( _
sShell As String, _
Optional ByVal eWindowStyle As VBA.VbAppWinStyle = vbNormalFocus, _
Optional ByRef sError As String, _
Optional ByVal lTimeOut As Long = 2000000000) As Boolean
'On Error GoTo errHandler
Dim ProcessID As Long
Dim hProcess As Long
Dim lRet As Long
'Dim lTimeStart As Long
Dim bSuccess As Boolean
Dim i As Long
bSuccess = False
ProcessID = Shell(sShell, eWindowStyle)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)
If (hProcess = 0) Then
sError = "This program could not determine whether the process started. Please watch the program and check it completes."
' Only fail if there is an error - this can happen when the program completes too quickly.
Else
bSuccess = True
'lTimeStart = timeGetTime()
Do
i = i + 1
' Get the status of the process
GetExitCodeProcess hProcess, lRet
' Sleep during wait to ensure the other process gets processor slice:
DoEvents
Sleep 1000
'If (timeGetTime() - lTimeStart > lTimeOut) Then
' ' Too long!
' sError = "The process has timed out."
' lRet = 0
' bSuccess = False
'End If
If i > 5 Then
lRet = 0
bSuccess = False
Debug.Assert False
End If
Loop While lRet = STATUS_PENDING
End If
endHandler:
ShellAndWait = bSuccess
Exit Function
errHandler:
'ErrorIn "modActivate.ShellAndWait(sShell,eWindowStyle,sError,lTimeOut)", Array(sShell, _
' eWindowStyle, sError, lTimeOut)
End Function
Usage:
If ShellAndWait(sExeFile, vbNormalFocus) Then
'Process has completed, so...
Else
'Process failed.
End If
Hope this helps.
.
- References:
- How to tell the end of a shell
- From: inquirer
- How to tell the end of a shell
- Prev by Date: Re: How to tell the end of a shell
- Next by Date: SQL query question
- Previous by thread: Re: How to tell the end of a shell
- Next by thread: Re: How to tell the end of a shell
- Index(es):
Relevant Pages
|