|
-
Jun 29th, 2005, 05:31 PM
#1
Thread Starter
Addicted Member
Reading the Program's task ID[RESOLVED]
I'm trying to execute another application that downloads data using the shell function. Because the shell function runs asynchronously, I need my program to wait until my download is complete before proceeding.
I have searched and read a few topics regarding an API function ShellAndWait, but I could not get my program to work correctly.
Is there another way? When I assign the task ID to a variable, is there another function I could launch to see if my task ID is still live?
Any help on this would be very much appreciated. Thanks in advance.
Rob
Last edited by robbie; Jun 30th, 2005 at 11:04 AM.
Reason: Problem Solved!!!
-
Jun 29th, 2005, 07:22 PM
#2
Addicted Member
Re: Reading the Program's task ID
Hi,
Perhaps this example may help:
VB Code:
' Add a reference to Windows Script Host Object Model
Dim RetVal As Long
Dim WSH As IWshShell
Dim WaitForTermination As Boolean
Dim CommandLine As String
Set WSH = New IWshShell_Class
CommandLine = "notepad.exe"
WaitForTermination = True
RetVal = WSH.Run(CommandLine, vbNormalFocus, WaitForTermination)
MsgBox "Notepad Closed!", vbInformation, "Program Termination"
Set WSH = Nothing
Have a good one!
BK
-
Jun 29th, 2005, 08:12 PM
#3
Thread Starter
Addicted Member
Re: Reading the Program's task ID
Thanks, but I get the automation error - The system cannot find the file specified. It launches the file using the shellandwait method, the only problem is that the program does not wait until my download completes and terminates.
Here is my standard module that has the api functions and the public shellandwait sub procedure:
VB Code:
Option Explicit
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal _
dwDesiredAccess As Long, ByVal bInherithandle As Long, ByVal _
dwProcessID As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal _
dwMilliseconds As Long)
Private Const INFINITE = &HFFFF
Private Const SYNCHRONIZE = &H100000
Private Const WAIT_TIMEOUT = &H102
Public Sub ShellAndWait(PathName, Optional WindowStyle As _
VbAppWinStyle = vbMinimizedFocus, Optional bDoEvents As _
Boolean = False)
Dim dwProcessID As Long
Dim hProcess As Long
dwProcessID = Shell(PathName, WindowStyle)
If dwProcessID = 0 Then
Exit Sub
End If
hProcess = OpenProcess(SYNCHRONIZE, False, dwProcessID)
If hProcess = 0 Then
Exit Sub
End If
If bDoEvents Then
Do While WaitForSingleObject(hProcess, 100) = WAIT_TIMEOUT
DoEvents
Loop
Else
WaitForSingleObject hProcess, INFINITE
End If
CloseHandle hProcess
End Sub
Here is my calling procedure which is in my main standard module:
VB Code:
Public Sub Main()
Dim Start
Let ds = "G:\Rob\Deposit\Database\Deposits.mdb"
Let cs = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ds & _
";Persist Security Info=False"
Set cn = New ADODB.Connection
cn.Open cs
Let DOC_DIRECT_LOC = "C:\Program Files\Mobius\DDR\rdswin.exe "
Let SCRIPT_LOC = "G:\Rob\Deposit\Scripts\098EFSMD.msl /A"
Let RPT_LOC = "G:\Rob\Deposit\RPT Files\098EFSMD.RPT"
Let TEXT_FILE = "G:\Rob\Deposit\CSV Files\098EFSMD.CSV"
Let Start = Now
ShellAndWait DOC_DIRECT_LOC & SCRIPT_LOC, vbMaximizedFocus, True
MsgBox "Started: " & Start & vbCrLf & "Finished: " & Now
Parse_EFSMD RPT_LOC, TEXT_FILE, cn
End Sub
What am I missing? I set the variable Start right before calling ShellAndWait. That way, when the message box is displayed, the Started Time and Finished Time must NOT BE THE SAME. This program loads and runs a script that download two decent sized files that takes around a minute to run. The problem is, when my script completes, it automatically terminates itself, then, I'm staring at a message box with the same start time as finish time.
-
Jun 29th, 2005, 08:43 PM
#4
Thread Starter
Addicted Member
Re: Reading the Program's task ID
I'm wondering if I can solve this simply by placing the script file in a batch file and call the batch file. The script file should automatically execute its container program and execute the script inside of it.
One other point is that this app that I am writing has no user interface. I only am using the message box just to see if I am using this procedure correctly. My app has two standard modules. One module with my main procedure and another module with a library of sub programs and functions.
-
Jun 29th, 2005, 09:51 PM
#5
Thread Starter
Addicted Member
Re: Reading the Program's task ID
Does it matter on the type of modules I am using in order for ShellAndWait to work properly? I have no form modules in this application. I'm using two standard modules. I kept the API functions private.
Any thoughts...
-
Jun 30th, 2005, 09:13 AM
#6
Thread Starter
Addicted Member
Re: Reading the Program's task ID
OK.
I did figure out why this procedure does not work for me. With task manager up and running, I run my code. I setup a breakpoint on the line that calls the OpenProcess API function. The third argument in the call is the processID that the Shell function assigns. What I noticed is that the processID that is assigned and the actual running processID is different, thus, ShellAndWait assumes the process is complete because it does not have the same processID that the shell function assigned.
Example: In VB, my shell function returns processID 2184. In task manager, the actual running process is 2284. Does anybody know why this is? Do I have to assign a different argument in one of the other arguments that is sent to OpenProcess?
-
Jun 30th, 2005, 11:10 AM
#7
Thread Starter
Addicted Member
Re: Reading the Program's task ID[RESOLVED]
Thanks to everyone putting up with the problem I was having in regards to ShellAndWait. After digging, and digging, and stepping though code, I determined I was testing for the wrong ProcessID.
The program I was using actually has two executable files. The first file (Which is the one I was testing), actually launches another executable that is the main container.
All the first exe does in lauch a pretty splash screen! I feel like a jerk.
The main exe program has a different name and once I shelled the main exe, the ProcessID's matched and the ShellAndWait procedure worked as advertised.
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
|