Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Delphi - Attendere il termine di un processo per proseguire
Forum - Delphi - Attendere il termine di un processo per proseguire

Avatar
systemgvp (Normal User)
Expert


Messaggi: 296
Iscritto: 14/04/2008

Segnala al moderatore
Postato alle 14:29
Venerdì, 06/04/2012
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

  1. [CODE]
  2.  
  3. ShellExecute(0,'open',pchar(cartellaprogramma+'Conv7z.exe'),pchar(parametri),nil,0);
  4.  
  5. //mostra dati
  6.   Memo2.Lines.LoadFromFile(cartellaTemporanea+'\'+NomeProgetto+'\progetto.txt');
  7.  
  8. [/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?

PM Quote
Avatar
Goblin (Member)
Expert


Messaggi: 375
Iscritto: 02/02/2011

Segnala al moderatore
Postato alle 14:42
Venerdì, 06/04/2012
In archivio ho trovato questa, non  ricordo dove l'ho presa, magari fa al caso tuo

Codice sorgente - presumibilmente Delphi

  1. function WinExecAndWait(FileName:String; Visibility : integer):Cardinal;
  2. var
  3.   StartupInfo:TStartupInfo;
  4.   ProcessInfo:TProcessInformation;
  5. begin
  6.   FillChar(StartupInfo,Sizeof(StartupInfo),#0);
  7.   StartupInfo.cb := Sizeof(StartupInfo);
  8.   StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  9.   StartupInfo.wShowWindow := Visibility;
  10.   if not CreateProcess(nil,
  11.     PChar(FileName),             { pointer to command line string }
  12.     nil,                  { pointer to process security attributes}
  13.     nil,                  { pointer to thread security attributes }
  14.     false,                { handle inheritance flag }
  15.     CREATE_NEW_CONSOLE or { creation flags }
  16.     NORMAL_PRIORITY_CLASS,
  17.     nil,                  { pointer to new environment block }
  18.     nil,                  { pointer to current directory name }
  19.     StartupInfo,          { pointer to STARTUPINFO }
  20.     ProcessInfo) then     { pointer to PROCESS_INF }
  21.       Result := 0
  22.   else
  23.   begin
  24.     WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
  25.     GetExitCodeProcess(ProcessInfo.hProcess,Result);
  26.   end;
  27. end;



Ibis redibis non morieris in bello
PM Quote
Avatar
smanettone83 (Normal User)
Pro


Messaggi: 124
Iscritto: 20/10/2010

Segnala al moderatore
Postato alle 15:12
Venerdì, 06/04/2012

prova con
Codice sorgente - presumibilmente Delphi

  1. try
  2. ShellExecute(0,'open',pchar(cartellaprogramma+'Conv7z.exe'),pchar(parametri),nil,0);
  3. finally
  4. Memo2.Lines.LoadFromFile(cartellaTemporanea+'\'+NomeProgetto+'\progetto.txt');
  5. end;



Ultima modifica effettuata da smanettone83 il 06/04/2012 alle 15:12
PM Quote
Avatar
systemgvp (Normal User)
Expert


Messaggi: 296
Iscritto: 14/04/2008

Segnala al moderatore
Postato alle 12:07
Sabato, 07/04/2012
Grazie, ho risolto con:

Codice sorgente - presumibilmente Delphi

  1. [CODE]
  2. uses ShellAPI, Winapi.Windows
  3.  
  4. procedure ApriEseguibileConAttesaFine(NomeDelFile: string; Parametri: string);
  5.  
  6. var exInfo: TShellExecuteInfo; Ph: DWORD;
  7. begin
  8.   FillChar(exInfo, SizeOf(exInfo), 0);
  9.   with exInfo do
  10.   begin
  11.     cbSize := SizeOf(exInfo);
  12.     fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;
  13.     Wnd := GetActiveWindow();
  14.     ExInfo.lpVerb := 'open';
  15.     ExInfo.lpParameters := PChar(Parametri);
  16.     lpFile := PChar(NomeDelFile);
  17.     nShow := 0;
  18.   end;
  19.   if ShellExecuteEx(@exInfo) then Ph := exInfo.HProcess
  20.   else begin ShowMessage(SysErrorMessage(GetLastError)); Exit; end;
  21.   while WaitForSingleObject(ExInfo.hProcess, 50) <> WAIT_OBJECT_0 do Application.ProcessMessages;
  22.   CloseHandle(Ph);
  23. end;
  24.  
  25. ApriEseguibileConAttesaFine('Applicazione.exe', 'parametri');
  26. [/CODE]


PM Quote