Results 1 to 4 of 4

Thread: Using PID to Wait for Proc to Exit

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Location
    RTP
    Posts
    10
    The fact that someone started a thread to ask which is better, if/else or case switch bothers me a little. The fact that no one had the right answer makes me leary of asking this question here, but I have few other sources:

    I am kluging the hell out of this code to get it to ftp to the mainframe. I have hunted and hunted and ftping seems to be something VB can handle in only the most superficial of manners.

    My way around this is to get VB to launch the batch to spawn the ftp session. When I do this I grab the PID of the cmd that the batch runs in. What I want to do now is wait for that process to exit. I have developed another kluge for this, but I am starting to run out of duct tape, and neither of these methods are stable or robust enough to recover from an error.

    My question is; what is the most robust way of waiting for the process to exit when you have not a handle, but a PID?

    -Travis
    Travis, BS in CSC
    MQ/Series Administration
    VB6E on NT4sp4

  2. #2
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Try WaitForSingleObject api. I used it in a batch processing app, and it has never failed.

    Code:
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Public Const WAIT_TIMEOUT = &H102&
    
    'a little piece of code
        retVal = WaitForSingleObject(ProcessHandle, 0)
        If retVal <> WAIT_TIMEOUT Then
            ' app is finished
            ' close the handle of the process
            Call CloseHandle(ProcessHandle)
        End If

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Oh yeah, I forgot something.
    My app periodically calls this routine to check if it is finished yet. It goes on doing other stuff in the mean time.
    If you want to wait until the other program is finished, and don't want to check periodically, you could pass INFINITE instead of 0 for dwMilliseconds. But your app will freeze until the other prog is finished (no repainting of the forms either).

    Public Const INFINITE = &HFFFF

    retVal = WaitForSingleObject(ProcessHandle, INFINITE)
    ' if you get here the process is finished

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2000
    Location
    RTP
    Posts
    10

    Handle same as PID?

    I thought WaitforSingleObject&() was using a handle that I had to the process, one from a CreateProcess&().

    I was using Wait when I was using Create, but Create would spawn a shell with no environmental variables inherited. I would preffer using Create if someone knows how to fix that. Right now I am using shell().

    I will play around with Wait.

    -Travis
    Travis, BS in CSC
    MQ/Series Administration
    VB6E on NT4sp4

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