Results 1 to 7 of 7

Thread: Reading the Program's task ID[RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Location
    Milwaukee, Wisconsin, USA
    Posts
    133

    Resolved 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!!!

  2. #2
    Addicted Member
    Join Date
    May 2005
    Posts
    168

    Re: Reading the Program's task ID

    Hi,

    Perhaps this example may help:

    VB Code:
    1. ' Add a reference to Windows Script Host Object Model
    2.    Dim RetVal As Long
    3.    Dim WSH As IWshShell
    4.    Dim WaitForTermination As Boolean
    5.    Dim CommandLine As String
    6.  
    7.    Set WSH = New IWshShell_Class
    8.    CommandLine = "notepad.exe"
    9.    WaitForTermination = True
    10.    RetVal = WSH.Run(CommandLine, vbNormalFocus, WaitForTermination)
    11.    MsgBox "Notepad Closed!", vbInformation, "Program Termination"
    12.    Set WSH = Nothing

    Have a good one!
    BK

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Location
    Milwaukee, Wisconsin, USA
    Posts
    133

    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:
    1. Option Explicit
    2.  
    3. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
    4.     hHandle As Long, ByVal dwMilliseconds As Long) As Long
    5.    
    6. Private Declare Function OpenProcess Lib "kernel32" (ByVal _
    7.     dwDesiredAccess As Long, ByVal bInherithandle As Long, ByVal _
    8.     dwProcessID As Long) As Long
    9.  
    10. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
    11.     As Long) As Long
    12.  
    13. Private Declare Sub Sleep Lib "kernel32" (ByVal _
    14.     dwMilliseconds As Long)
    15.  
    16. Private Const INFINITE = &HFFFF
    17. Private Const SYNCHRONIZE = &H100000
    18. Private Const WAIT_TIMEOUT = &H102
    19.  
    20. Public Sub ShellAndWait(PathName, Optional WindowStyle As _
    21.    VbAppWinStyle = vbMinimizedFocus, Optional bDoEvents As _
    22.    Boolean = False)
    23.  
    24.     Dim dwProcessID As Long
    25.     Dim hProcess As Long
    26.    
    27.     dwProcessID = Shell(PathName, WindowStyle)
    28.    
    29.     If dwProcessID = 0 Then
    30.         Exit Sub
    31.     End If
    32.    
    33.     hProcess = OpenProcess(SYNCHRONIZE, False, dwProcessID)
    34.    
    35.     If hProcess = 0 Then
    36.         Exit Sub
    37.     End If
    38.    
    39.     If bDoEvents Then
    40.         Do While WaitForSingleObject(hProcess, 100) = WAIT_TIMEOUT
    41.             DoEvents
    42.         Loop
    43.     Else
    44.         WaitForSingleObject hProcess, INFINITE
    45.     End If
    46.    
    47.     CloseHandle hProcess
    48.    
    49. End Sub

    Here is my calling procedure which is in my main standard module:
    VB Code:
    1. Public Sub Main()
    2.  
    3.     Dim Start
    4.    
    5.     Let ds = "G:\Rob\Deposit\Database\Deposits.mdb"
    6.     Let cs = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ds & _
    7.              ";Persist Security Info=False"
    8.  
    9.     Set cn = New ADODB.Connection
    10.     cn.Open cs
    11.  
    12.     Let DOC_DIRECT_LOC = "C:\Program Files\Mobius\DDR\rdswin.exe "
    13.     Let SCRIPT_LOC = "G:\Rob\Deposit\Scripts\098EFSMD.msl /A"
    14.     Let RPT_LOC = "G:\Rob\Deposit\RPT Files\098EFSMD.RPT"
    15.     Let TEXT_FILE = "G:\Rob\Deposit\CSV Files\098EFSMD.CSV"
    16.  
    17.     Let Start = Now
    18.     ShellAndWait DOC_DIRECT_LOC & SCRIPT_LOC, vbMaximizedFocus, True
    19.     MsgBox "Started: " & Start & vbCrLf & "Finished: " & Now
    20.     Parse_EFSMD RPT_LOC, TEXT_FILE, cn
    21.  
    22. 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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Location
    Milwaukee, Wisconsin, USA
    Posts
    133

    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Location
    Milwaukee, Wisconsin, USA
    Posts
    133

    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...

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Location
    Milwaukee, Wisconsin, USA
    Posts
    133

    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?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Location
    Milwaukee, Wisconsin, USA
    Posts
    133

    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
  •  



Click Here to Expand Forum to Full Width