Bonjour,

Contexte :
J'ai besoin d'utiliser un utilitaire (mailsend.exe) pour envoyer des emails, mais sans bloquer l'application.
J'utilise CreateProcess avec un TimeOut pour éviter d'avoir à programmer une temporisation pour tuer le processus.
Important (!) : Delphi 5


J'ai un problème avec CreateProcess quand je l'exécute à l'intérieur d'un thread.
Le code ci-dessous fonctionnait sans problème à l'extérieur.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
constructor TThreadMail.Create(CreateSuspended: Boolean);
begin
  inherited Create(CreateSuspended);
  FreeOnTerminate := true;
  Priority := tpNormal;
end;
 
procedure TThreadMail.Execute;
var
  options, fic : string;
  StartInfo : TStartupInfo;
  ProcInfo : TProcessInformation;
  CreateOK : Boolean;
  prog	: string;
  timeout : integer;
begin
  options := '...';
  timeout := 5000;
  prog := CHEMIN + 'mailsend.exe' + options;
  UniqueString(prog);
  FillChar(StartInfo,SizeOf(TStartupInfo),#0);
  FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
  StartInfo.cb := SizeOf(TStartupInfo);
  //ProcInfo.hThread := Handle;
  CreateOK := CreateProcess(nil, PChar(prog), 
              nil, nil,False,
              DETACHED_PROCESS,
              nil, nil, StartInfo, ProcInfo);
  try
    if CreateOK then begin   // <==== TOUJOURS FAUX !!!
      if timeout > 0 then WaitForSingleObject(ProcInfo.hProcess, timeout); // INFINITE);
    end;
  finally
    CloseHandle(ProcInfo.hProcess);
    CloseHandle(ProcInfo.hThread);
  end;
end;
 
// ---------
// in DataModule 
begin  
  ...
  with TThreadMail.Create(TRUE) do begin
    FreeOnTerminate := TRUE; 
    Priority := tpNormal;
    // exécution
    Resume; 
  end;
  ...
end;