Forum - Delphi
- Attendere il termine di un processo per proseguire
systemgvp (Normal User)
Expert
Messaggi: 296
Iscritto: 14/04/2008
Salve
Io utilizzo un'applicazione esterna al mio progetto, per estrarre dei file presenti in un archivio compresso e, una volta estratti, mi mostra il contenuto di uno di essi, in questo modo:
Codice sorgente - presumibilmente Delphi
[ CODE]
ShellExecute( 0 , 'open' , pchar ( cartellaprogramma+ 'Conv7z.exe' ) , pchar ( parametri) , nil , 0) ;
//mostra dati
Memo2. Lines . LoadFromFile ( cartellaTemporanea+ '\' + NomeProgetto+ '\progetto.txt' ) ;
[ / CODE]
il problema è che affinchè possa verificarsi il LoadFromFile è necessario che il file sia stato già creato, ovvero attendere che tutto il processo di estrazione sia finito. E' possibile "sospendere" la mia applicazione finchè l'applicazione avviata con ShellExecute() non abbia avuto termine?
Goblin (Member )
Expert
Messaggi: 375
Iscritto: 02/02/2011
In archivio ho trovato questa, non ricordo dove l'ho presa, magari fa al caso tuo
Codice sorgente - presumibilmente Delphi
function WinExecAndWait( FileName: String ; Visibility : integer ) : Cardinal ;
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
FillChar ( StartupInfo, Sizeof ( StartupInfo) , #0 ) ;
StartupInfo. cb : = Sizeof ( StartupInfo) ;
StartupInfo. dwFlags : = STARTF_USESHOWWINDOW;
StartupInfo. wShowWindow : = Visibility;
if not CreateProcess( nil ,
PChar ( FileName) , { pointer to command line string }
nil , { pointer to process security attributes}
nil , { pointer to thread security attributes }
false , { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil , { pointer to new environment block }
nil , { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then { pointer to PROCESS_INF }
Result : = 0
else
begin
WaitforSingleObject( ProcessInfo. hProcess , INFINITE) ;
GetExitCodeProcess( ProcessInfo. hProcess , Result) ;
end ;
end ;
Ibis redibis non morieris in bello
smanettone83 (Normal User)
Pro
Messaggi: 124
Iscritto: 20/10/2010
prova con
Codice sorgente - presumibilmente Delphi
try
ShellExecute( 0 , 'open' , pchar ( cartellaprogramma+ 'Conv7z.exe' ) , pchar ( parametri) , nil , 0) ;
finally
Memo2. Lines . LoadFromFile ( cartellaTemporanea+ '\' + NomeProgetto+ '\progetto.txt' ) ;
end ;
Ultima modifica effettuata da smanettone83 il 06/04/2012 alle 15:12
systemgvp (Normal User)
Expert
Messaggi: 296
Iscritto: 14/04/2008
Grazie, ho risolto con:
Codice sorgente - presumibilmente Delphi
[ CODE]
uses ShellAPI, Winapi. Windows
procedure ApriEseguibileConAttesaFine( NomeDelFile: string ; Parametri: string ) ;
var exInfo: TShellExecuteInfo; Ph: DWORD ;
begin
FillChar ( exInfo, SizeOf ( exInfo) , 0) ;
with exInfo do
begin
cbSize : = SizeOf ( exInfo) ;
fMask : = SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;
Wnd : = GetActiveWindow( ) ;
ExInfo. lpVerb : = 'open' ;
ExInfo. lpParameters : = PChar ( Parametri) ;
lpFile : = PChar ( NomeDelFile) ;
nShow : = 0 ;
end ;
if ShellExecuteEx( @ exInfo) then Ph : = exInfo. HProcess
else begin ShowMessage( SysErrorMessage ( GetLastError ) ) ; Exit ; end ;
while WaitForSingleObject( ExInfo. hProcess , 50) <> WAIT_OBJECT_0 do Application. ProcessMessages ;
CloseHandle( Ph) ;
end ;
ApriEseguibileConAttesaFine( 'Applicazione.exe' , 'parametri' ) ;
[ / CODE]