Autor Beitrag
lemmond1976
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66



BeitragVerfasst: So 13.04.03 17:27 
Hallo,

wie kann ich erreichen, daß mein Programm solange wartet bis eine bestimmte Datei nicht mehr benutzt wird (also freigegeben ist) und dann mit dieser Datei was macht?

Es kann nämlich vorkommen, daß mehrere User zur gleichen Zeit auf diese Datei zugreifen wollen.

Die Funktion IsFileinUse liefert TRUE, wenn die Datei benutzt wird. "GOTO" möchte ich vermeiden.

Viele Grüße
Gunnar
lemmond1976 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66



BeitragVerfasst: Mo 14.04.03 00:12 
Mir würden auch Stichwörter genügen.
Google-Suche brachte mir nichts !
Danke !
toms
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1099
Erhaltene Danke: 2



BeitragVerfasst: Mo 14.04.03 07:37 
ausblenden Quelltext
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:
function IsFileInUse(FileName: TFileName): Boolean; 
var 
  HFileRes: HFILE; 
begin 
  Result := False; 
  if not FileExists(FileName) then Exit; 
  HFileRes := CreateFile(PChar(FileName), 
                         GENERIC_READ or GENERIC_WRITE, 
                         0, 
                         nil, 
                         OPEN_EXISTING, 
                         FILE_ATTRIBUTE_NORMAL, 
                         0); 
  Result := (HFileRes = INVALID_HANDLE_VALUE); 
  if not Result then 
    CloseHandle(HFileRes); 
end; 


procedure TForm1.Button1Click(Sender: TObject); 
begin 
  if IsFileInUse('c:\Programs\delphi6\bin\delphi32.exe') then 
    ShowMessage('File is in use.'); 
  else 
    ShowMessage('File not in use.'); 
end;


Source:
www.swissdelphicente.../showcode.php?id=104
lemmond1976 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66



BeitragVerfasst: Mo 14.04.03 10:28 
hi Tom, danke für Deine Antwort.
Die Funktion ist mir durchaus geläufig.
Okay, es ist unwahrscheinlich, daß der Fall eintritt, aber was ist, wenn zwei oder mehrere User zur gleichen Zeit (auf die Sekunde genau) die Funktion benutzen und abfragen wollen, ob die Datei benutzt wird ?
Dann wird wahrscheinlich bei allen Usern ein True zurückkommen.
Tja, und das ist nicht gut.
Hätte jemand eine Idee, wie ich den Zugriff "sicher" machen kann ?
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Mo 14.04.03 10:53 
Bin mir zwar nicht sicher, aber ich glaube schon, dass Windows die CreateFile-Aufrufe intern synchronisiert...

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
lemmond1976 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66



BeitragVerfasst: Mo 14.04.03 10:59 
Achso, das wäre nicht schlecht... ;-)
lemmond1976 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66



BeitragVerfasst: Mo 14.04.03 11:48 
wenn intern eine Synchronisierung stattfindet, dann dürfte man doch so eigentlich ein Warten erzwingen, bis die Datei nicht mehr benutzt wird.


ausblenden Quelltext
1:
2:
3:
4:
5:
6:
   sprung: while FileIsInUse('Filename') = TRUE do 
            begin
             goto sprung;
            end;

            //schreibe in die Datei


Wie ginge das ohne "goto" ?
Danke für eine Antwort !

Gruss
Gunnar
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Mo 14.04.03 11:54 
Autsch, ein goto! ;)

Ganz einfach:
ausblenden Quelltext
1:
2:
3:
4:
while FilIsInUse(<FileName>) do
begin
  Sleep(1); // damit die Warteschleife nicht die ganze Zeit die CPU "besetzt"
end;

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
lemmond1976 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66



BeitragVerfasst: Mo 14.04.03 12:27 
hi, ja, wie gesagt, goto ist auch nicht unbedingt mein Ding, aber sleep ist okay. Danke. Dann müßte es eigentlich funzen.
McToast
Hält's aus hier
Beiträge: 3

Win98, mu-Linux
D3 Prof
BeitragVerfasst: Fr 21.11.03 20:18 
Titel: Sleep in Delphi 3 ? [gefunden!]
Hallo!

Ich habe eine Nachfrage hierzu:

Gibt es diese Möglichkeit eigentlich auch in Delphi 3 ... oder:
Wie kann ich sie emulieren ?

ausblenden Quelltext
1:
2:
3:
4:
while FilIsInUse(<FileName>) do
begin
  Sleep(1); // damit die Warteschleife nicht die ganze Zeit die CPU "besetzt"
end;


geändertert Text:

Habe die Lösung gefunden! - Meine Online-Hilfe zeigt sie scheinbar nicht immer an???!? - Selbstversuch klappte!

McToast