On Fri, 24 Apr 2009, Francesco Saverio Giudice wrote:

Hi,

> is it possible to add below function ?
> hb_ProcessIsRunning( hProc, nTimeoutMsecs ) -> lIsRunning
> to check if a process started with hb_ProcessOpen() is currently running so 
> I can have:
> PROC Main()
>   LOCAL hOut
>   LOCAL hProc := hb_ProcessOpen( "test.exe",, @hOut, @hOut, .T. )
>   LOCAL nStart := hb_milliseconds()
>   LOCAL cDate, cOutPut, nLen
>   IF hProc > -1
>      DO WHILE .T.
>         cData := Space( 1000 )
>         cOutPut := ""
>         DO WHILE ( nLen := Fread( hOut, @cData, Len( cData ) ) ) > 0
>            cOutPut += SubStr( cData, 1, nLen )
>            cData := Space( 1000 )
>         ENDDO

Here last fread() returned 0 so the process has just finished execution.
It's not necessary to use hb_ProcessIsRunning() because hOut PIPE is using
blocking IO.

>         IF !hb_ProcessIsRunning( hProc, 0 )
>            EXIT
>         ENDIF

You never reach this place because hb_ProcessIsRunning() should always
return FALSE.
>         IF ( hb_milliseconds() - nStart ) > 30 * 1000
>            // Kill process
>            HB_ProcessClose( hProc )
>         ENDIF
>      ENDDO
>      HB_ProcessValue( hProc )
>
>   ENDIF
> RETURN
> Without need to handle it in MT mode using another thread as I did in 
> uhttpd and using above code in ST mode.

Maybe in real windows some thins works in different way but for me it looks
that you are trying to make sth different then you really need.
This is your code modified to work as self contain example.

   request HB_GT_CGI_DEFAULT
   proc main(x)
      local hOut, hProc, cData, nLen
      if x == nil
         hProc := hb_ProcessOpen( hb_progname() + " X",, @hOut, @hOut, .T. )
         if hProc != -1
            ? "process open"
            cData := Space( 1000 )
            while  ( nLen := Fread( hOut, @cData, Len( cData ) ) ) > 0
               ? "read[" + left( cData, nLen ) + "]"
            enddo
            ? "nLen = 0 -> process finished with exit code:", ;
              HB_ProcessValue( hProc )
            ?
         endif
      else
         for nLen := 1 to 5
            outstd( "line " + hb_ntos( nLen ) )
            hb_idleSleep( 0.3 )
         next
         outstd( "EOF" )
         errorlevel( 57 )
      endif
   return

It should show if hOut is using blocking IO or not.
BTW PIPEs without blocking IO are usually very hard to use so if they are
not blocking calling process in windows we should try to fix it.

> int hb_fsProcessIsRunning( HB_FHANDLE hProcess, DWORD dwTimeout )
> {
>    BOOL fRunning = FALSE;
>
>    HB_TRACE(HB_TR_DEBUG, ("hb_fsProcessIsRunning(%p, %d)", ( void * ) ( 
> HB_PTRDIFF ) hProcess, dwTimeout));
>
> {
>    DWORD dwResult;
>    HANDLE hProc = ( HANDLE ) hb_fsGetOsHandle( hProcess );
>
>    if( hProc )
>    {
>       hb_vmUnlock();
>       dwResult = WaitForSingleObject( hProc, dwTimeout );
>       if ( dwResult != WAIT_OBJECT_0 )
>          fRunning = TRUE;
>       hb_vmLock();
>    }
>    else
>       hb_fsSetError( ( USHORT ) FS_ERROR );
> }
>    return fRunning;
> }
>
> and for hbprocfn.c
>
> HB_FUNC( HB_PROCESSISRUNNING )
> {
>    HB_FHANDLE hProcess = hb_numToHandle( hb_parnint( 1 ) );
>
>    if( hProcess != 0 && hProcess != FS_ERROR && ( hb_pcount() < 2 || ISNUM( 
> 2 ) ) )
>       hb_retni( hb_fsProcessValue( hProcess, ( hb_pcount() < 2 ? 0 : 
> hb_parnint( 2 ) ) ) );
>    else
>       hb_errRT_BASE_SubstR( EG_ARG, 4001, NULL, HB_ERR_FUNCNAME, 
> HB_ERR_ARGS_BASEPARAMS );
> }
> but for other OSes I don't know how handle it.
> Could you kindly help me ?

Similar functionality but without timeout parameter:

   function hb_ProcessIsRunning( hProc )
   return hb_processValue( hPorc, .f. ) != -1

But I think that for final application you need sth different and the
above request is result of some current behavior misunderstanding.

best regards,
Przemek
_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to