Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2003.11.27;
Скачать: CL | DM;

Вниз

ReadDirectoryChangesW   Найти похожие ветки 

 
Kair ©   (2003-10-01 11:18) [0]

unit FirstThread;

interface

uses Windows, SysUtils, Classes, Variants;

type
TFindNotifyInformation = record
NextEntryOffset: DWord;
Action: DWord;
FileNameLength: DWord;
FileName: array [0..255] of WideChar;
end;

TSysLogerThread = class(TThread)
procedure UpdateLog;
procedure StartLog;
procedure StopLog;
private
{ Private declarations }
cbReturn: Cardinal;
protected
FNotifier: Cardinal;
FPath: String;
FNotifyBuffer: TFindNotifyInformation;
procedure Execute; override;
public
Notifier: Cardinal read FNotifier;
Path: string read FPath;
NotifyBuffer: TFindNotifyInformation read FNotifyBuffer;
constructor Create(APath: String);
destructor Destroy; override;
end;

implementation

uses fMain;

constructor TSysLogerThread.Create(APath: String);
begin
inherited Create(True);
FPath:=APath;
FreeOnTerminate:=True;
Priority:=tpNormal;
Resume;
end;

destructor TSysLogerThread.Destroy;
begin
inherited Destroy;
end;

procedure TSysLogerThread.Execute;
begin
FNotifier:=CreateFile(PChar(FPath),GENERIC_READ,
FILE_SHARE_READ or FILE_SHARE_DELETE or
FILE_SHARE_DELETE,nil,OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,0);
Win32Check(FNotifier<>INVALID_HANDLE_VALUE);
Synchronize(StartLog);
while WaitForSingleObject(FNotifier,INFINITE)=WAIT_OBJECT_0 do
begin
if ReadDirectoryChangesW(FNotifier,@FNotifyBuffer,1024,True,
FILE_NOTIFY_CHANGE_FILE_NAME,@cbReturn,nil,nil) then
Synchronize(UpdateLog);
end;
Synchronize(StopLog);
end;

procedure TSysLogerThread.UpdateLog;
begin
case FNotifyBuffer.Action of
FILE_ACTION_ADDED: Main.fLoger.Items.Add(TimeToStr(Time)+": "+FNotifyBuffer.FileName+" (создание файла)");
FILE_ACTION_REMOVED: Main.fLoger.Items.Add(TimeToStr(Time)+": "+FNotifyBuffer.FileName+" (удаление файла)");
FILE_ACTION_MODIFIED: Main.fLoger.Items.Add(TimeToStr(Time)+": "+FNotifyBuffer.FileName+" (изменение файла)");
FILE_ACTION_RENAMED_OLD_NAME: Main.fLoger.Items.Add(TimeToStr(Time)+": "+FNotifyBuffer.FileName+" (файл переименован)");
FILE_ACTION_RENAMED_NEW_NAME: Main.fLoger.Items.Add(TimeToStr(Time)+": "+FNotifyBuffer.FileName+" (файл переименован)");
end;
if Main.fLoger.Items.Count>200 then
Main.fLoger.Items.Delete(0);
end;

procedure TSysLogerThread.StartLog;
begin
Main.fLoger.Items.Add(TimeToStr(Time)+": "+"Запущен");
Main.Stb.Panels[1].Text:="Включен";
end;

procedure TSysLogerThread.StopLog;
begin
Main.fLoger.Items.Add(TimeToStr(Time)+": "+"Остановлен");
Main.Stb.Panels[1].Text:="Отключен";
Main.Stop.Enabled:=False;
Main.Start.Enabled:=True;
end;

end.

Функция ReadDirectoryChangesW: чета у меня не очень получается ее использовать. И при этом еще и поток не могу остановить. А когда в процедуре UpdateLog делаю
Main.fLoger.Items.Add(TimeToStr(Time)+": "+FNotifyBuffer.FileName+" (создание файла)");
то там новый путь к файлу наслаивается на предыдущий и так и выводится.


 
clickmaker ©   (2003-10-01 11:38) [1]

Чтобы поток остановить, можно завести объект типа Event и юзать WaitForMultipleObjects FNotifier + хэндл event"a. Надо остановить поток, просто ставишь event в сигнальное состояние.


 
Kair ©   (2003-10-06 07:36) [2]

Ну, вот, к примеру, запустил программу, создаю файл "Текстовый файл.txt" и выходит надпись
15:00:10: C:\XXX\Текстовый файл.txt (создание файла)
Затем переименовываю его в "12.txt" и выходит надпись
15:01:35: C:\XXX\12.txtвый файл.txt (файл переименован)


 
Radionov Alexey ©   (2003-10-06 08:22) [3]

>Kair © (06.10.03 07:36) [2]
по поводу последнего (наслаивания) добавь
в UpdateLog:

With FNotifyBuffer Do
FileName[FileNameLength div 2] := #0;

По поводу "не могу остановить" - естественно. Твой поток не планируется, пока не произойдет модификация выбранного каталога (WaitForSingleObject)


 
pasha_golub ©   (2003-10-06 10:02) [4]

while (WaitForSingleObject(FNotifier,INFINITE)=WAIT_OBJECT_0) and not Terminated do ...



Страницы: 1 вся ветка

Текущий архив: 2003.11.27;
Скачать: CL | DM;

Наверх




Память: 0.48 MB
Время: 0.022 c
4-90158
LG
2003-10-05 04:33
2003.11.27
Как вывести список файлов текущей директории


6-90039
SaML
2003-10-01 09:42
2003.11.27
Помогите плиз...


1-89830
romeo
2003-11-18 15:35
2003.11.27
Все время черно-белая картинка!!! Блин...


14-90117
PavelSin
2003-11-04 09:26
2003.11.27
Просмоторщик дайджестов


3-89742
robert
2003-11-07 22:37
2003.11.27
помогите с sql