|
-
Mar 4th, 2003, 11:54 AM
#1
Thread Starter
New Member
How to know when an external program ended?
Hello to all, this is my first post in this forum. Ok here we go....I need a little help with this problem
***********************************
Dim Value
Value = Shell("xxxxxx.exe", vbNormalFocus)
'***********************************
The above line is used by me to call up an external program call "xxxxxx.exe". What this program does is process some results, and output the result to a data file, and closed itself.
The content of the file will be read in the subsequent lines.
----------------------------------------------------------
Note: By default, the Shell function runs other programs asynchronously. This means that a program started with Shell might not finish executing before the statements following the Shell function are executed. ~taken from MSDN
---------------------------------------------------------
Now my question to fellow VB experts are, how in VB codes can I know when the program closes. I need to run the next line only after the program closed. It is important cuz the next line has a high chance of running even before the data file is generated. The data file must be generated before the next line can be run. Hope u guys can help. Thanks.
When it is dark enough, you can see the stars.
-
Mar 4th, 2003, 12:29 PM
#2
Frenzied Member
Look up the WaitForSingleObject API:
The recommended way is to use CreateProcess & WaitForSingleObject.
VB Code:
'This program needs a common dialog box, named CDBox
' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
' and select Microsoft Common Dialog control)
Const INFINITE = &HFFFF
Const STARTF_USESHOWWINDOW = &H1
Private Enum enSW
SW_HIDE = 0
SW_NORMAL = 1
SW_MAXIMIZE = 3
SW_MINIMIZE = 6
End Enum
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 Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Enum enPriority_Class
NORMAL_PRIORITY_CLASS = &H20
IDLE_PRIORITY_CLASS = &H40
HIGH_PRIORITY_CLASS = &H80
End Enum
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Function SuperShell(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean
Dim pclass As Long
Dim sinfo As STARTUPINFO
Dim pinfo As PROCESS_INFORMATION
'Not used, but needed
Dim sec1 As SECURITY_ATTRIBUTES
Dim sec2 As SECURITY_ATTRIBUTES
'Set the structure size
sec1.nLength = Len(sec1)
sec2.nLength = Len(sec2)
sinfo.cb = Len(sinfo)
'Set the flags
sinfo.dwFlags = STARTF_USESHOWWINDOW
'Set the window's startup position
sinfo.wShowWindow = start_size
'Set the priority class
pclass = Priority_Class
'Start the program
If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, _
0&, WorkDir, sinfo, pinfo) Then
'Wait
WaitForSingleObject pinfo.hProcess, dwMilliseconds
SuperShell = True
Else
SuperShell = False
End If
End Function
Private Sub Form_Load()
'KPD-Team 1998
'URL: [url]http://www.allapi.net/[/url]
'Set the dialog's title
CDBox.DialogTitle = "Choose an EXEC-File ..."
'Error when canceled
CDBox.CancelError = True
'Set the dialog's filter
CDBox.Filter = "EXEC-Files (*.exe)|*.exe|All files (*.*)|*.*"
'Show the 'Open File'-dialog
CDBox.ShowOpen
'Execute the program
SuperShell CDBox.filename, Left$(CDBox.filename, Len(CDBox.filename) - Len(CDBox.FileTitle)), 0, SW_NORMAL, HIGH_PRIORITY_CLASS
End
End Sub
-
Mar 4th, 2003, 09:41 PM
#3
Thread Starter
New Member
Thanks Jordan.
I don't need a dialog box to open exexcutable files though. "xxxxx.exe" is all I need to run. I will try it out later in the day to see if there is any problem.
When it is dark enough, you can see the stars.
-
Mar 4th, 2003, 11:23 PM
#4
Fanatic Member
Just an idea, but declare value as long, shell only returns the process id of an executeable being ran so a variant really isnt needed, which is what you do when you leave a variable undeclared.
-
Mar 5th, 2003, 06:38 AM
#5
Thread Starter
New Member
Thanks, the below are the codes I used eventually, and ya, it works.
---------------------------------------------------------------------------------
Public Function SyncShellCmd(CmdStr As String, ApplicationFocus As Long) As Boolean
'Run the application by the CmdStr, and exit when the program terminated, Application focus specific
'the focus of the application. e.g vbNormalFocus
Dim lPid As Long 'The task ID of the started program
Dim lHnd As Long ' handle to object to wait for
Dim lRet As Long 'Return value of WaitForSingleObject()
SyncShellCmd = False
If CmdStr = "" Then Exit Function
lPid = Shell(CmdStr, ApplicationFocus)
If lPid <> 0 Then
'Get a handle to the shelled process.
lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
'If successful, wait for the application to end and close the handle.
If lHnd <> 0 Then
lRet = WaitForSingleObject(lHnd, INFINITE) 'no time out
CloseHandle (lHnd)
SyncShellCmd = True
End If
'MsgBox "Just terminated.", vbInformation, "Shelled Application"
End If
End Function
------------------------------------------------------------------------------
When it is dark enough, you can see the stars.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|