Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "WinAPI";
Текущий архив: 2003.11.27;
Скачать: [xml.tar.bz2];

Вниз

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 вся ветка

Форум: "WinAPI";
Текущий архив: 2003.11.27;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.46 MB
Время: 0.017 c
8-90014
Darlock
2003-07-22 16:49
2003.11.27
Печать картинки


1-89990
Dmitriy M. Volkov
2003-11-17 14:15
2003.11.27
FindNext - поиск заново


1-89845
WCM
2003-11-16 18:51
2003.11.27
Создание объектов


3-89714
AlexiNx
2003-11-03 18:31
2003.11.27
Fast Report


7-90155
Дмитрий
2003-09-17 14:27
2003.11.27
TVaComm в win2k и win98





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский