Gostaria de compartilhar com vocês uma função que criei aqui para executar processos externos. Segue abaixo um exemplo e a função:
Código: Selecionar todos
/*
Exemplo de uso da função MyRunProc
By Rossine
*/
#include "fileio.ch"
function Main
cls
hb_memowrit( "teste.txt", "Arquivo de teste..." )
? "Retorno:", myRunProc( "notepad teste.txt", .T., { || qout( "Aguardando Notepad encerrar..." ) }, 1 )
wait "Quando chegar aqui o notepad ja foi encerrado. Tecle ENTER..."
? "Retorno:", myRunProc( "notepad teste.txt", .F. )
wait "Quando chegar aqui o notepad ainda estara rodando. Tecle ENTER..."
? "Retorno:", myRunProc( "notepad teste.txt",, { |nCtd| qout( "Aguardando Notepad encerrar (" + hb_ntos(5-nCtd) + ")..." ) } ,, 5 )
wait "O Loop foi encerrado apos em 5 segundos..."
? "Retorno:", myRunProc( "notepad teste.txt",, { |nCtd|
qout( "Forcando o encerramento do Notepad atraves do retorno do codeblock..." )
return iif( nCtd = 2, .F., .T. )
} )
wait "O Loop foi encerrado..."
return NIL
/*
Parametros: cExecute - Comando a ser executado.
lWait - Se é para o executor aguardar o encerramento do processo iniciado.
bBlock - Bloco que será executado enquanto o processo estiver sendo executado.
nTime - De quanto em quanto tempo será verificado se o processo já terminou.
nOccurs - Encerra o loop de espera após X vezes que o loop estiver processando.
*/
******************
function myRunProc( cExecute, lWait, bBlock, nTime, nOccurs )
******************
local hProcess, nResult, nCtd := 0, lRet
__DefaultNIL( @lWait, .T. )
__DefaultNIL( @nTime, 1 )
__DefaultNIL( @nOccurs, 0 )
hProcess := hb_processOpen( cExecute )
if hProcess != F_ERROR
do while ( nResult := hb_processValue( hProcess, .F. ) ) == -1
if !lWait
exit
endif
nCtd += 1
if hb_IsBlock( bBlock )
lRet := eval( bBlock, nCtd )
if hb_IsLogical( lRet ) .and. !lRet
hb_processClose( hProcess, .T. )
exit
endif
endif
if nTime > 0
hb_idleSleep( nTime )
endif
if nCtd = nOccurs
hb_processClose( hProcess, .T. )
exit
endif
enddo
endif
return nResult
// EOF //
Qualquer opnião sobre melhoria da mesma ou correção será bem vinda.
Como uso harbour, não sei se irá rodar com xHarbour. Alguém poderia confirmar se roda no xHarbour também ?
Obrigado,
Rossine.


