Threads et notifications de création de fichier
J'ai une application client Ole en Delphi qui imprime une série de documents Office en PDF.
La sérialisation est gérée au moyen de threads surveillant un dossier:
1 - un thread de surveillance est lancé juste avant l'impression de chaque document
2 - le thread est terminé (et libéré) quand le fichier PDF est produit
3 - quand le thread est terminé, le document suivant est imprimé
Le probleme est:
Sur la moitié des machines, le thread ne termine jamais ...
Voici le code de la méthode Execute
Code:
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 64 65
|
Procedure TThreadSurveillance.Execute;
var
Chemin :Array[0..255] Of Char;
Infos :TSearchRec;
begin
// Initialisation ds données du Thread
FHandleNotification := INVALID_HANDLE_VALUE;
// Boucle pincipale du Thread
while not Terminated Do
begin
if FCheminASurveiller <> FCheminEnCours then
begin
// Une demande est en cours, on libère le Handle
if FHandleNotification <> INVALID_HANDLE_VALUE then
begin
FindCloseChangeNotification(FHandleNotification);
FHandleNotification := INVALID_HANDLE_VALUE;
end;
// Prise en compte de la nouvelle demande
FCheminEnCours := FCheminASurveiller;
if FCheminEnCours <> '' then
begin
FCheminEnCours := ExcludeTrailingPathDelimiter(FCheminEnCours);
FHandleNotification := FindFirstChangeNotification(
StrPCopy(Chemin,FCheminEnCours), // Chemin à surveiler
False, // Ne pas surveiller les sous-répertoires
File_NOTIFY_CHANGE_File_NAME // Surveiller les changements de noms
+File_NOTIFY_CHANGE_LAST_WRITE); // et les écritures
// Mémorisation de l'heure de la demande
FDateHeureVerification:=Now;
end;
end;
if FHandleNotification <> INVALID_HANDLE_VALUE then
begin
//case WaitForSingleObject(FHandleNotification,200) Of
case WaitForSingleObject(FHandleNotification,INFINITE) Of
WAIT_Object_0:
begin
if SysUtils.FindFirst(FObjFileName.CompleteValue,faAnyFile,Infos)=0 then
begin
if FileDateToDateTime(Infos.Time) > FDateHeureVerification then
begin
sysutils.FindClose(Infos);
Synchronize(FichierGenere);
end;
sysutils.FindClose(Infos);
end;
end;
WAIT_TIMEOUT :;
end;
// Le handle doit être mis à jour pour pouvoir effectuer une nouvelle demande
FindNextChangeNotification(FHandleNotification);
end;
end; // while not terminated
// Libération du Handle en cas de besoin
if FHandleNotification <> INVALID_HANDLE_VALUE then
FindCloseChangeNotification(FHandleNotification);
FHandleNotification := INVALID_HANDLE_VALUE;
end; |