const
WM_MY_USER = WM_USER + 101;
type
PInfoRecord = ^TInfoRecord ;
TInfoRecord =
record
GUID :
string;
Meldung :
string;
end;
TWatchThread =
class(TThread)
private
fHandle : HWND;
procedure fisFileNotifyDirectoryChanged(Sender: TObject);
public
OneBackup : TOneBackup;
FileNotify : TfisFileNotification;
FileZip : TJvZlibMultiple;
procedure StartWatch;
protected
procedure DoTerminate;
override;
public
constructor Create(aOwner : TComponent;
Handle : HWND);
virtual;
end;
TForm1 =
class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
procedure OnEvent(
var Message : TMessage );
message WM_MY_USER;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
ObjectList : TObjectList;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TWatchThread.DoTerminate;
begin
inherited;
FileNotify.Stop;
end;
procedure TWatchThread.fisFileNotifyDirectoryChanged(Sender: TObject);
var
InfoRecord : PInfoRecord;
begin
if not Terminated
then
begin
New(InfoRecord);
InfoRecord^.GUID := OneBackup.GUID;
InfoRecord^.Meldung := OneBackup.
Name + '
- ' + FormatDateTime('
hh:nn:ss.zzz', now) + '
- ' +
'
FileNotify.MessageNo';
PostMessage(fHandle, WM_MY_USER, 1, Integer(InfoRecord));
// hier müssen die Änderungen abgespeichert werden
// funktioniert noch nicht
// FileZip.CompressDirectory(OneBackup.WatchFolder,
// OneBackup.IncludeSub,
// OneBackup.BackupFolder + '\' + OneBackup.GUID + '.zip');
end;
end;
procedure TWatchThread.StartWatch;
begin
FileNotify.Subtree := OneBackup.IncludeSub;
FileNotify.Start;
end;
constructor TWatchThread.Create(aOwner : TComponent;
Handle : HWND);
var
NewGUID: TGUID;
begin
inherited Create(true);
freeOnTerminate := true;
fHandle :=
Handle;
OneBackup := TOneBackup.Create;
FileNotify := TfisFileNotification.Create(aOwner);
FileNotify.OnDirectoryChanged := fisFileNotifyDirectoryChanged;
with OneBackup
do
begin
CreateGUID(NewGUID);
GUID := GUIDToString(NewGUID);
Name := '
';
WatchFolder := '
C:\Test\Treadtest';
// nur mal zum Testen
BackupFolder := '
C:\Test';
// nur mal zum Testen
...
FileNotify.Directory := WatchFolder;
FileNotify.NotificationType := [noFilename, noDirname, noSize, noLastWrite, noCreation];
end;
FileZip := TJvZlibMultiple.Create(aOwner);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
WatchThread : TWatchThread;
begin
WatchThread := TWatchThread.Create(Self, Self.Handle);
// hier die Daten übergeben
WatchThread.OneBackup.
Name := Edit1.Text;
TWatchThread(ObjectList.Items[ObjectList.Add(WatchThread)]).Start;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i : integer;
begin
for i := 0
to ObjectList.Count - 1
do
begin
TWatchThread(ObjectList.Items[i]).Terminate;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
TWatchThread(ObjectList.Items[ObjectList.Count - 1]).StartWatch;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ObjectList := TObjectList.Create;
ObjectList.OwnsObjects := false;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ObjectList.Free;
end;
procedure TForm1.OnEvent(
var Message : TMessage);
var
InfoRecord : PInfoRecord;
ID :
string;
begin
case Message.Msg
of
WM_MY_USER :
begin
try
InfoRecord := PInfoRecord(
Message.LParam);
ID := InfoRecord^.GUID;
Memo1.Lines.Add(InfoRecord^.Meldung);
finally
Dispose(InfoRecord);
end;
end;
end;
end;