Bonjour,

Dans le cadre d'un projet, j'ai mis la partie impression de documents dans un Thread. En retour un utilisateur (l'utilisateur principal) m'a indiqué que les impressions ne se faisait pas (ou du moins se perdaient dans les limbes, ça c'est mon interprétation).
Du coup, après épluchage du code du thread (ou du moins de la fonction appelée) j'ai vu qu'aucun TPrinter (que j'utilise) n'était créé

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
51
52
53
54
55
56
57
58
59
60
61
62
63
function Imprimer(const Annee: String; const Numero: Integer): Boolean;
// imprimer un Stream { TODO : Non testée }
var
  NomFichier: String;
  FromPage, ToPage, Page: Integer;
  FirstPage: Boolean;
  AStream: TMemoryStream;
  Bitmap: TBitmap;
  R: TRectF;
  fpdfrun: TFPdf;
  fpdfViewRun: TFPdfView;
  Printer : TPrinter;
begin
  Result := False;
 // ... chargement du stream (pdf)
    if AStream.Size > 0 then
    begin
      Printer:=TPrinter.Create;

      fpdfrun := TFPdf.Create(nil);
      fpdfViewRun := TFPdfView.Create(nil);
      try
        fpdfrun.FileName := NomFichier;
        fpdfViewRun.Pdf := fpdfrun;
        fpdfViewRun.Active := True;
        FromPage := 1;
        ToPage := fpdfrun.PageCount;
        Printer.Title := NomFichier;
        FirstPage := True;
        Printer.BeginDoc;
        try
          for Page := FromPage to ToPage do
          begin
            if FirstPage then
              FirstPage := False
            else
              Printer.NewPage;
            fpdfViewRun.PageNumber := Page;
            Bitmap := fpdfViewRun.RenderPage(0, 0, Printer.PageWidth,
              Printer.PageHeight, ro0, [rePrinting]);
            try
              R := TRectF.Create(0, 0, Printer.PageWidth, Printer.PageHeight);
              Printer.Canvas.DrawBitmap(Bitmap, R, R, 1.0, True);
            finally
              Bitmap.Free;
            end;
          end;
        finally
          Printer.EndDoc;
        end;
      finally
        fpdfViewRun.Active := False;
        FreeAndNil(fpdfViewRun);
        FreeAndNil(fpdfrun);
        FreeAndNil(Printer);
      end;
      Result := True;
    end;
   //  fin d'impression, mise à jour historique ... 
  finally
    AStream.Free;
  end;
end;
à la va vite, j'ai rajouté la création d'un TPrinter (code gras, souligné) mais je m'interroge sur
a) le bien fondé
b) l'initialisation du TPrinter
c) la "sécurité" (thread safe) de TPrinter

Quelqu'un a t-il déjà fait face à ça ?