Bonjour,
J'ai développé deux programmes qui utilisent un répertoire commun pour s'échanger des informations via des fichiers texte.
Le problème qui se pose, c'est que parfois, j'ai des erreurs E/S lorsqu'un fichier est en cours d'écriture par le premier programme et que le second programme essaie de le lire puis de le supprimer.
Ci-dessous le code qui écrit le fichier :
Ci-dessous le code qui lit le fichier puis le supprime :
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 procedure TMain.FormCreate(Sender: TObject); var Ini : TIniFile; Repertoire : string; Fichier : TextFile; PathFichier : string; begin if ParamCount = 2 then begin try Log(ParamStr(1) + ' ' + ParamStr(2)); Ini := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); Repertoire := Ini.ReadString('General', 'RepertoireTicket', 'C:\'); Ini.Free; PathFichier := Repertoire + FormatDateTime('yyyymmddhhnnsszzz',Now()) + '.txt'; AssignFile(Fichier, PathFichier); if FileExists(PathFichier) then Append(Fichier) else Rewrite(Fichier); WriteLn(Fichier, ParamStr(1) + ' ' + ParamStr(2)); CloseFile(Fichier); Application.Terminate; except on E : Exception do begin Log(E.Message); Application.Terminate; end; end; end else Application.Terminate; end;
Savez-vous ce que je dois faire pour éviter ce genre d'erreur ?
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 procedure TMain.Timer1Timer(Sender: TObject); var SR : TSearchRec; Fichier : TextFile; Ligne : string; begin Timer1.Enabled := false; if IdTCPClient1.Connected then begin if FindFirst(ExtractFilePath(Application.ExeName) + 'Tickets\*.txt', faAnyFile, SR)=0 then begin repeat AssignFile(Fichier, ExtractFilePath(Application.ExeName) + 'Tickets\' + SR.FindData.cFileName); Reset(Fichier); case IOResult of 0 : begin while not EOF(Fichier) do begin ReadLn(Fichier,Ligne); IdTCPClient1.IOHandler.WriteLn(Ligne); Log(Format('Envoi du ticket %s : %s',[SR.FindData.cFileName, Ligne])); end; CloseFile(Fichier); DeleteFile(ExtractFilePath(Application.ExeName) + 'Tickets\' + SR.FindData.cFileName); end; end; until FindNext(SR) <> 0; FindClose(SR); end; end else begin Log(Format('Tentative de connexion au serveur TCP : %s/%d (TimeOut : %d)',[IdTCPClient1.Host, IdTCPClient1.Port, IdTCPClient1.ConnectTimeout])); try IdTCPClient1.Connect; except on E : Exception do begin Log(E.Message); end; end; end; Timer1.Enabled := true; end;
Merci,
ZiP
Partager