Hi Przemek and Mindaugas,

Il 24/04/2009 13.34, Przemyslaw Czerpak ha scritto:

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


thank you for explanation and for tests, but I was too fast in writing
and I did few tests so I didn't saw that hOut PIPE is using blocking IO.

What I need is to run an application and kill it if it uses more time
than necessary or hangs.
I'm writing an application that has to run on some PCs in an unattended
way and it has to run some external applications to make some upgrades.
Because I don't know the state of these PCs there is the possibility
that some of those applications will not complete themselves or will use
too much time and I need to kill them.
In MT mode I think that I have found a correct solution as I implemented
in uhttpd (but if you have time please check me looking at CGIExec /
CGIKill functions), but some of those PCs have Win 9x installed and MT
is not supported (application built on Vista give error probably due to some missing dependency in Win9x and I would like to have same executable for all platforms) so I thought to try using ST mode. But I miss blocking IO (I thought that was not blocking) and also wrote some errors in code I posted.

Now I have done more extensive tests (I hope enough) and I have seen that I cannot use FRead() version because if application doesn't write something (in case it write anything or hangs) it will wait forever.
So the code should be (I have modified your sample):

   request HB_GT_CGI_DEFAULT
   proc main(x)
      local hOut, hProc, cData, nLen
      LOCAL nStart

      if x == nil

         ? "starting process"
         hProc := hb_ProcessOpen( hb_progname() + " X",,,, .T. )
         if hProc != -1

            ? "process open"
            nStart := hb_milliseconds()

            while .t.
               ? "Checking application status: "
               IF hb_ProcessIsRunning( hProc, 1000 )
                  ?? " Process is running"
                  IF ( hb_milliseconds() - nStart ) > 10 * 1000
                     ? "Killing process"
                     HB_ProcessClose( hProc )
                     EXIT
                  ENDIF
               ELSE
                  ?? " Process is NOT running"
                  EXIT
               ENDIF
            enddo
            ? "process finished with exit code: "
            ?? HB_ProcessValue( hProc )
            ?
         endif
      else
         for nLen := 1 to 5
            outstd( "line " + hb_ntos( nLen ) )
            hb_idleSleep( 3 )
         next
         outstd( "EOF" )
         errorlevel( 57 )
      endif
   return

and in this case result is:

-------
J:\cvs\harbourSVN_trunk\tests>tstkillapp1

starting process
process open
Checking application status:  Process is running
Checking application status:  Process is running
Checking application status:  Process is running
Checking application status:  Process is running
Checking application status:  Process is running
Checking application status:  Process is running
Checking application status:  Process is running
Checking application status:  Process is running
Checking application status:  Process is running
Checking application status:  Process is running
Killing process
process finished with exit code:          0
-------

where hb_ProcessIsRunning() I have changed to:

   FUNCTION hb_ProcessIsRunning( hProc, nTimeout )
      LOCAL lIsRunning := ( HB_ProcessValue( hProc, .f. ) == -1 )
      IF HB_ISNUMERIC( nTimeout ) .AND. nTimeout > 0
         hb_idleSleep( nTimeout / 1000 )
      ENDIF
   RETURN lIsRunning


I hope this mail could be useful to others.
Thank you for your time and I will appreciate if you will tell me if it is correct or there are other ways to implement it in ST mode.

Best Regards,
Francesco
_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to