A better "shell and wait"

m0use

Registered User.
Local time
Today, 10:42
Joined
Mar 23, 2004
Messages
23
I hope you will find this information usefull.

I use Outlook to receive reports and then automatically open the right access-database (with shell and command parameters) to process them.
In my search for a way to stop my Outlook-code with processing untill the access-database has closed, and then continue, I found many posts about "shell and wait". I tried this but it does not work for me because I want to perform other tasks while I wait for the dbase to close.

I found out that the WaitForSingleObject() function can better be replaced by the GetExitCodeProcess() function because, and I quote:
"Its disadvantage over the GetExitCodeProcess method is that, during the wait period, the VB application is totally unresponsive. This includes not processing screen updates (i.e. if a portion of the app is covered/uncovered), nor does it allow any means to move the form or minimize the window."

For more information please read the article "GetExitCodeProcess: Determine when a Shelled App has Ended" in this link:
Code:
http://vbnet.mvps.org/index.html?code/faq/getexitcprocess.htm

For an example see the code below.



Code:
Option Compare Database
Option Explicit

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 Declare Function CloseHandle Lib "kernel32" _
  (ByVal hObject As Long) As Long

Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const STATUS_PENDING = &H103&


Public Function checkShell(Optional procID As Long)

    Dim hProcess As Long
    Dim processID As Long
    Dim exitCode As Long

    'processID = procID
    processID = Shell("c:\windows\notepad.exe")
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, processID)

    Do
        Call GetExitCodeProcess(hProcess, exitCode)
        DoEvents
    Loop While exitCode = STATUS_PENDING

    Call CloseHandle(hProcess)

    MsgBox "The shelled process " & processID & " has ended."

End Function


Greetings, m0use
 

Users who are viewing this thread

Back
Top Bottom