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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
| unit ThreadSurFile;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, SyncObjs;
type
PFILE_NOTIFY_INFORMATION = ^TFILE_NOTIFY_INFORMATION;
FILE_NOTIFY_INFORMATION = packed record
NextEntryOffset : DWORD;
Action : DWORD;
FileNameLength : DWORD;
FileName : array [0..1023] of WideChar;
end;
TFILE_NOTIFY_INFORMATION = FILE_NOTIFY_INFORMATION;
TProcess = procedure of object;
TThreadFile = class;
TListTStrings = array of TStrings;
TThreadSurveillanceDirectory = class(TThread)
private
{ Déclarations privées }
FDirectoryInProgress : string;
FDirectoryMonitorSrc : string;
FUpdateTStrings1 : TStrings;
FUpdateTStrings2 : TStrings;
FTimeWait : Integer;
// FProcess : TProcess;
HandleDirectory : THandle;
OverLapped : TOverlapped;
ListInfo : TStrings;
dwShareMode : Cardinal;
FFollowedFile : Integer;
// FThread : THandle;
ThreadFile : TThreadFile;
ArrayThreadFile : TThreadList;
List : TList;
FhLogWnd : HWND;
FdwNotifyFilter : Cardinal;
EventThread : TEvent;
procedure SetdwNotifyFilter(Value: Cardinal);
procedure SetDirectoryMonitorSrc(Value: string);
procedure MAJAffichage;
procedure WMUserStop(var Msg: TMessage); message WM_USER;
// procedure ThreadProcess;
protected
{ Déclarations protégées }
public
// constructor Create(const aDirectoryMonitorSrc: string; aUpdateTStrings: TListTStrings = nil; aFollowedFile: Integer = -1;
// aProcess: TProcess = nil; adwNotifyFilter: Cardinal = 19; aTimeWait : Integer = 1000);
constructor Create(const aDirectoryMonitorSrc: string; aUpdateTStrings: TListTStrings = nil; aFollowedFile: Integer = -1;
adwNotifyFilter: Cardinal = 19; aTimeWait : Integer = 1000);
destructor Destroy; override;
procedure Execute; override;
published
property dwNotifyFilter : Cardinal read FdwNotifyFilter write SetdwNotifyFilter default 19;
property DirectoryMonitorSrc : string read FDirectoryMonitorSrc write SetDirectoryMonitorSrc;
end;
TThreadSurveillanceFile = class(TThread)
private
{ Déclarations privées }
FFileNameProgress : TStringList;
FFileNameSrc : TStringList;
FUpdateTStrings1 : TStrings;
FUpdateTStrings2 : TStrings;
FTimeWait : Integer;
OverLapped : TOverlapped;
// FProcess : TProcess;
ListInfo : TStrings;
FFollowedFile : Integer;
// FThread : THandle;
ThreadFile : TThreadFile;
ArrayThreadFile : TThreadList;
List : TList;
FhLogWnd : HWND;
EventThread : TEvent;
procedure SetFileNameSrc(Value: TStringList);
procedure MAJAffichage;
procedure WMUserStop(var Msg: TMessage); message WM_USER;
// procedure ThreadProcess;
protected
{ Déclarations protégées }
public
// constructor Create(const aFileName: TStringList; aUpdateTStrings: TStrings = nil; aFollowedFile: Integer = -1;
// aProcess: TProcess = nil);
constructor Create(const aFileName: TStringList; aUpdateTStrings: TListTStrings = nil;
aFollowedFile: Integer = -1; aTimeWait : Integer = 1000);
destructor Destroy; override;
procedure Execute; override;
published
property FileName : TStringList read FFileNameSrc write SetFileNameSrc;
end;
TThreadFile = class(TThread)
private
{ Déclarations privées }
ListInfo : TStrings;
FUpdateTStrings : TStrings;
FTimeWait : Integer;
FFileName : string;
hRecMsg : THandle;
ReceiveStop : Integer;
hLogWnd : HWND;
FFollowedFile : Integer;
procedure MAJAffichage;
procedure WMUpdateData(var Msg: TMessage); message WM_USER;
protected
constructor CreateSurv(const aFileName: string; aUpdateTStrings: TStrings = nil;
aFollowedFile: Integer = -1; aTimeWait: Integer = 1000; aRecMsg: THandle = INVALID_HANDLE_VALUE);
public
constructor Create(const aFileName: string; aUpdateTStrings: TStrings = nil; aTimeWait: Integer = 1000);
destructor Destroy; override;
procedure Execute; override;
published
end;
const
fomNotFound = $01;
fomReadable = $02;
fomWritable = $04;
FILE_LIST_DIRECTORY = $00000001;
WM_UPDATESTOP = WM_USER + 100;
WM_UPDATEDATA = WM_USER + 101;
tfFileNotReadNotWrite = 0;
tfFileNotFound = 1;
tfFileReadNotWrite = 2;
tfFileNotReadWrite = 4;
tfFileReadWrite = 6;
implementation
{****************************************************************************}
{****************************************************************************}
{******************************** TThreadFile *******************************}
{****************************************************************************}
{****************************************************************************}
function OpenFileInfo(const aFileName: string): Integer;
var
FileHandle : integer;
begin
Result := 0;
//Test si le fichier existe
if not FileExists(aFileName) then
begin
Result := fomNotFound;
Exit;
end;
//Test si le fichier peut être ouvert en lecture
FileHandle := FileOpen(aFileName, fmOpenRead or fmShareDenyWrite);
if FileHandle >= 0 then
begin
FileClose(FileHandle);
Result := Result or fomReadable;
end;
//Test si le fichier peut être ouvert en écriture
FileHandle := FileOpen(aFileName, fmOpenWrite or fmShareDenyNone); //or fmShareDenyRead);
if FileHandle >= 0 then
begin
FileClose(FileHandle);
if Result <> 0 then //Pour empêcher la validation pour le fmShareDenyRead
Result := Result or fomWritable;
end;
end;
constructor TThreadFile.CreateSurv(const aFileName: string; aUpdateTStrings: TStrings = nil;
aFollowedFile: Integer = -1; aTimeWait: Integer = 1000; aRecMsg: THandle = INVALID_HANDLE_VALUE);
begin
IsMultiThread := True;
inherited Create(False);
if aUpdateTStrings <> nil then
FUpdateTStrings := aUpdateTStrings;
FFileName := aFileName;
FTimeWait := aTimeWait;
hRecMsg := aRecMsg;
IsMultiThread := True;
Priority := tpNormal;
hLogWnd := AllocateHWnd(WMUpdateData);
FFollowedFile := aFollowedFile;
end;
//aFileName : Fichier à surveiller
//aUpdateTStrings : TStrings pour recevoir des infos de TThreadFile
//aTimeWait : Temps de scrutation du thread
constructor TThreadFile.Create(const aFileName: string; aUpdateTStrings: TStrings = nil; aTimeWait: Integer = 1000);
begin
IsMultiThread := True;
inherited Create(False);
if aUpdateTStrings <> nil then
FUpdateTStrings := aUpdateTStrings;
FFileName := aFileName;
FTimeWait := aTimeWait;
ReceiveStop := 0;
hRecMsg := INVALID_HANDLE_VALUE;
IsMultiThread := True;
Priority := tpNormal;
hLogWnd := AllocateHWnd(WMUpdateData);
end;
procedure TThreadFile.MAJAffichage;
begin
if FUpdateTStrings <> nil then
begin
FUpdateTStrings.BeginUpdate;
FUpdateTStrings.AddStrings(ListInfo);
FUpdateTStrings.EndUpdate;
end;
end;
procedure TThreadFile.Execute;
var
ofInfoResult : Integer;
ResFile : string;
begin
inherited;
ListInfo := TStringList.Create;
try
while not Terminated do
begin
ListInfo.Clear;
ofInfoResult := OpenFileInfo(FFileName);
ResFile := '';
//Lecture des 3 bits pour exploiter le résultat
{if Odd(T) then
ResFile := 'Not Found (1)'
else
begin
T := T shr 1;
if Odd(T) then
ResFile := ResFile + 'Readable (2) '
else
ResFile := ResFile + 'Not Readable (2) ';
T := T shr 1;
if Odd(T) then
ResFile := ResFile + 'Writable (4)'
else
ResFile := ResFile + 'Not Writable (4)';
end;
ListInfo.Add(Format('%s > Statut : %s',[FFileName, ResFile]));}
//ou lecture par la valeur
case ofInfoResult of
0 : ListInfo.Add(Format('%s > Statut : Not Readable (2) Not Writable (4)',[FFileName]));
1 : ListInfo.Add(Format('%s > Statut : Not Found',[FFileName]));
2 : ListInfo.Add(Format('%s > Statut : Readable (2) Not Writable (4)',[FFileName]));
4 : ListInfo.Add(Format('%s > Statut : Not Readable (2) Writable (4)',[FFileName]));
6 : ListInfo.Add(Format('%s > Statut : Readable (2) Writable (4)',[FFileName]));
end;
Synchronize(MAJAffichage);
if hRecMsg <> INVALID_HANDLE_VALUE then
begin
if FFollowedFile = ofInfoResult then
begin
PostMessage(hRecMsg, WM_UPDATESTOP, Self.Handle, ofInfoResult);
Terminate;
end;
end;
Sleep(FTimeWait);
end;
finally
ListInfo.Free;
end;
end;
procedure TThreadFile.WMUpdateData(var Msg: TMessage);
begin
if Msg.Msg = WM_UPDATEDATA then
ReceiveStop := Msg.LParam;
end;
destructor TThreadFile.Destroy;
begin
if hLogWnd <> INVALID_HANDLE_VALUE then
DeallocateHWnd(hLogWnd);
inherited;
end;
{****************************************************************************}
{****************************************************************************}
{************************* TThreadSurveillanceFile **************************}
{****************************************************************************}
{****************************************************************************}
{procedure TThreadSurveillanceFile.ThreadProcess;
begin
FProcess;
end;
function StartProcessFile(Sender: TThreadSurveillanceFile): integer; stdcall;
begin
try
Sender.ThreadProcess;
Result := 0;
except
Result := -1;
end;
end;}
procedure TThreadSurveillanceFile.SetFileNameSrc(Value: TStringList);
begin
if not FFileNameSrc.Equals(Value) then
begin
FFileNameSrc := Value;
EventThread.SetEvent;
end;
end;
procedure TThreadSurveillanceFile.WMUserStop(var Msg: TMessage);
var
// ThreadID : Cardinal;
I : Integer;
begin
if Msg.Msg = WM_UPDATESTOP then
begin
if Msg.LParam = FFollowedFile then
begin
if Msg.WParam <> 0 then
begin
List := ArrayThreadFile.LockList;
if List.Count <> 0 then
begin
for I := 0 to List.Count - 1 do
begin
ThreadFile := List.Items[0];
if ThreadFile.Handle = Msg.WParam then
Break;
end;
ThreadFile.Terminate;
{if Assigned(FProcess) then
begin
FThread := CreateThread(nil, 0, @StartProcessFile, Self, 0, ThreadID);
if WaitForSingleObject(FThread, 1000) = WAIT_TIMEOUT then
begin
CloseHandle(FThread);
end
else
begin
CloseHandle(FThread);
end;
end;}
ThreadFile.WaitFor;
FreeAndNil(ThreadFile);
List.Delete(I);
end;
ArrayThreadFile.UnlockList;
end;
end;
end;
end;
//aFileName : Liste de fichier à surveiller
//aUpdateTStrings : Liste de TStrings pour recevoir des infos TThreadSurveillanceFile et TThreadFile
//aFollowedFile : Type d'ouverture rechercher provoquant l'arrêt du thread
//aTimeWait : Temps de scrutation du thread
//aProcess : Pour lancer un process externe
//constructor TThreadSurveillanceFile.Create(const aFileName: TStringList; aUpdateTStrings: TStrings; aFollowedFile: Integer = -1;
// aProcess: TProcess = nil);
constructor TThreadSurveillanceFile.Create(const aFileName: TStringList; aUpdateTStrings: TListTStrings = nil;
aFollowedFile: Integer = -1; aTimeWait : Integer = 1000);
begin
IsMultiThread := True;
// inherited Create(CreateSuspended);
inherited Create(False);
// FreeOnTerminate := True;
if aUpdateTStrings <> nil then
begin
FUpdateTStrings1 := aUpdateTStrings[0];
if High(aUpdateTStrings) <> 1 then
FUpdateTStrings2 := aUpdateTStrings[0]
else
FUpdateTStrings2 := aUpdateTStrings[1]
end;
// FProcess := aProcess;
FTimeWait := aTimeWait;
FFileNameSrc := TStringList.Create;
FFileNameSrc.AddStrings(aFileName);
FhLogWnd := INVALID_HANDLE_VALUE;
EventThread := TEvent.Create(nil, False, False, 'EventThread');
Priority := tpNormal;
FFollowedFile := aFollowedFile;
// if CreateSuspended then
// Resume;
end;
destructor TThreadSurveillanceFile.Destroy;
var
I: Integer;
begin
FreeAndNil(EventThread);
if ArrayThreadFile <> nil then
begin
List := ArrayThreadFile.LockList;
for I := List.Count-1 downto 0 do
begin
ThreadFile := List.Items[I];
ThreadFile.Terminate;
ThreadFile.WaitFor;
FreeAndNil(ThreadFile);
end;
ArrayThreadFile.UnlockList;
ArrayThreadFile.Free;
end;
FFileNameSrc.Free;
if FhLogWnd <> INVALID_HANDLE_VALUE then
DeallocateHWnd(FhLogWnd);
inherited;
end;
procedure TThreadSurveillanceFile.Execute;
var
I, J : Integer;
NotSur : Boolean;
begin
inherited;
ListInfo := TStringList.Create;
FFileNameProgress := TStringList.Create;
OverLapped.hEvent := CreateEvent(nil, False, False, nil);
try
FFileNameProgress.Clear;
for I := 0 to FFileNameSrc.Count - 1 do
ListInfo.Add(Format('ThreadSurveillanceFile Start ! FileName : %s',[FFileNameSrc[I]]));
Synchronize(MAJAffichage);
while not Terminated do
begin
//Si changement configuration de surveillance
//Si changement de répertoire
if not (FFileNameSrc.Equals(FFileNameProgress)) then
begin
ListInfo.Clear;
if FhLogWnd = INVALID_HANDLE_VALUE then
FhLogWnd := AllocateHWnd(WMUserStop);
if ArrayThreadFile = nil then
ArrayThreadFile := TThreadList.Create;
List := ArrayThreadFile.LockList;
if List.Count = 0 then
FFileNameProgress.Clear;
FFileNameProgress.AddStrings(FFileNameSrc);
for I := 0 to FFileNameProgress.Count - 1 do
begin
if ArrayThreadFile <> nil then
ListInfo.Add(Format('Update ThreadSurveillanceFile ! FileName : %s',[FFileNameSrc[I]]));
//On ouvre la list de thread en cours
NotSur := False;
//On vérifie si le fichier n'est pas déjà suivie
for J := 0 to List.Count - 1 do
begin
ThreadFile := List.Items[J];
if ThreadFile.FFileName = FFileNameProgress[I] then
begin
NotSur := True;
Break;
end;
end;
if not NotSur then
begin
ThreadFile := TThreadFile.CreateSurv(FFileNameProgress[I], FUpdateTStrings2, FFollowedFile, FTimeWait, FhLogWnd);
ArrayThreadFile.Add(ThreadFile);
end;
end;
Synchronize(MAJAffichage);
ArrayThreadFile.UnlockList;
end;
//Attente d'une modification
repeat
//Création d'un événement pour relancer une nouvelle configuration
if EventThread.WaitFor(0) = wrSignaled then
I := 0;
//Pour recevoir les messages du threadfile
Application.ProcessMessages;
until Terminated or (not FFileNameSrc.Equals(FFileNameProgress)) or (List.Count = 0);
end;
finally
ListInfo.Free;
FFileNameProgress.Free;
CloseHandle(OverLapped.hEvent);
end;
end;
procedure TThreadSurveillanceFile.MAJAffichage;
begin
if FUpdateTStrings1 <> nil then
begin
FUpdateTStrings1.BeginUpdate;
FUpdateTStrings1.AddStrings(ListInfo);
FUpdateTStrings1.EndUpdate;
end;
end;
{****************************************************************************}
{****************************************************************************}
{*********************** TThreadSurveillanceDirectory ***********************}
{****************************************************************************}
{****************************************************************************}
{procedure TThreadSurveillanceDirectory.ThreadProcess;
begin
FProcess;
end;
function StartProcess(Sender: TThreadSurveillanceDirectory): integer; stdcall;
begin
try
Sender.ThreadProcess;
Result := 0;
except
Result := -1;
end;
end;}
procedure TThreadSurveillanceDirectory.SetdwNotifyFilter(Value: Cardinal);
begin
if FdwNotifyFilter <> Value then
begin
FdwNotifyFilter := Value;
EventThread.SetEvent;
end;
end;
procedure TThreadSurveillanceDirectory.SetDirectoryMonitorSrc(Value: string);
begin
if FDirectoryMonitorSrc <> Value then
begin
if DirectoryExists(FDirectoryMonitorSrc) then
begin
FDirectoryMonitorSrc := Value;
EventThread.SetEvent;
end;
end;
end;
procedure TThreadSurveillanceDirectory.WMUserStop(var Msg: TMessage);
var
// ThreadID : Cardinal;
I : Integer;
begin
if Msg.Msg = WM_UPDATESTOP then
begin
if Msg.LParam = FFollowedFile then
begin
if Msg.WParam <> 0 then
begin
List := ArrayThreadFile.LockList;
if List.Count <> 0 then
begin
for I := 0 to List.Count - 1 do
begin
ThreadFile := List.Items[I];
if ThreadFile.Handle = Msg.WParam then
Break;
end;
ThreadFile.Terminate;
{if Assigned(FProcess) then
begin
FThread := CreateThread(nil, 0, @StartProcess, Self, 0, ThreadID);
if WaitForSingleObject(FThread, 1000) = WAIT_TIMEOUT then
begin
CloseHandle(FThread);
end
else
begin
CloseHandle(FThread);
end;
end;}
ThreadFile.WaitFor;
FreeAndNil(ThreadFile);
List.Delete(I);
end;
ArrayThreadFile.UnlockList;
end;
end;
end;
end;
//aDirectoryMonitorSrc : répertoire de fichier à surveiller
//aUpdateTStrings : Liste de TStrings pour recevoir des infos TThreadSurveillanceDirectory et TThreadFile
//aFollowedFile : Type d'ouverture rechercher provoquant l'arrêt du thread
//adwNotifyFilter : Type de modifications surveillées dans le répertoire
//aTimeWait : Temps de scrutation du thread
//aProcess : Pour lancer un process externe
//constructor TThreadSurveillanceDirectory.Create(const aDirectoryMonitorSrc: string; aUpdateTStrings: TListTStrings = nil;
// aFollowedFile: Integer = -1; aProcess: TProcess = nil; adwNotifyFilter: Cardinal = 19; aTimeWait : Integer = 1000);
constructor TThreadSurveillanceDirectory.Create(const aDirectoryMonitorSrc: string; aUpdateTStrings: TListTStrings = nil;
aFollowedFile: Integer = -1; adwNotifyFilter: Cardinal = 19; aTimeWait : Integer = 1000);
begin
IsMultiThread := True;
// inherited Create(CreateSuspended);
inherited Create(False);
// FreeOnTerminate := True;
if aUpdateTStrings <> nil then
begin
FUpdateTStrings1 := aUpdateTStrings[0];
if High(aUpdateTStrings) <> 1 then
FUpdateTStrings2 := aUpdateTStrings[0]
else
FUpdateTStrings2 := aUpdateTStrings[1]
end;
// FProcess := aProcess;
FDirectoryMonitorSrc := aDirectoryMonitorSrc;
FhLogWnd := INVALID_HANDLE_VALUE;
FTimeWait := aTimeWait;
EventThread := TEvent.Create(nil, False, False, 'EventThread');
Priority := tpNormal;
if FdwNotifyFilter <> adwNotifyFilter then
FdwNotifyFilter := adwNotifyFilter;
FFollowedFile := aFollowedFile;
// if CreateSuspended then
// Resume;
end;
destructor TThreadSurveillanceDirectory.Destroy;
var
I: Integer;
begin
FreeAndNil(EventThread);
if ArrayThreadFile <> nil then
begin
List := ArrayThreadFile.LockList;
for I := List.Count-1 downto 0 do
begin
ThreadFile := List.Items[I];
ThreadFile.Terminate;
ThreadFile.WaitFor;
FreeAndNil(ThreadFile);
end;
ArrayThreadFile.UnlockList;
ArrayThreadFile.Free;
end;
if FhLogWnd <> INVALID_HANDLE_VALUE then
DeallocateHWnd(FhLogWnd);
inherited;
end;
procedure TThreadSurveillanceDirectory.Execute;
var
Buffer1 : FILE_NOTIFY_INFORMATION;
Buffer2 : FILE_NOTIFY_INFORMATION;
Return : DWord;
Action1 : DWord;
// Action2 : DWord;
Str1 : WideString;
Str2 : WideString;
Waiting : Boolean;
I : Integer;
NotSur : Boolean;
begin
inherited;
ListInfo := TStringList.Create;
FillChar(Overlapped, SizeOf(OverLapped), #0);
OverLapped.hEvent := CreateEvent(nil, False, False, nil);
try
HandleDirectory := INVALID_HANDLE_VALUE;
FDirectoryInProgress := '';
// dwNotifyFilter := FILE_NOTIFY_CHANGE_FILE_NAME or FILE_NOTIFY_CHANGE_DIR_NAME
// or FILE_NOTIFY_CHANGE_LAST_WRITE or FILE_NOTIFY_CHANGE_ATTRIBUTES;
dwShareMode := FILE_SHARE_READ or FILE_SHARE_DELETE or FILE_SHARE_WRITE;
ListInfo.Add(Format('ThreadSurveillanceDirectory Start ! Directory : %s',[FDirectoryMonitorSrc]));
Synchronize(MAJAffichage);
while not Terminated do
begin
//Si changement configuration de surveillance
//Si changement de répertoire
if (FDirectoryMonitorSrc <> FDirectoryInProgress) then
begin
ListInfo.Clear;
if HandleDirectory <> INVALID_HANDLE_VALUE then
begin
ListInfo.Add(Format('Update ThreadSurveillanceDirectory ! dwNotifyFilter : %d',[dwNotifyFilter]));
CloseHandle(HandleDirectory);
HandleDirectory := INVALID_HANDLE_VALUE;
end;
FDirectoryInProgress := FDirectoryMonitorSrc;
if FDirectoryInProgress <> '' then
begin
FDirectoryInProgress := ExcludeTrailingPathDelimiter(FDirectoryInProgress);
HandleDirectory := CreateFile(PWideChar(FDirectoryInProgress), FILE_LIST_DIRECTORY, dwShareMode,
nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS or FILE_FLAG_OVERLAPPED, 0);
end;
Synchronize(MAJAffichage);
end;
if HandleDirectory <> INVALID_HANDLE_VALUE then
begin
//ReadDirectoryChangesW permet de voir les changement dans un répertoire
//msdn.microsoft.com
if ReadDirectoryChangesW(HandleDirectory, @Buffer1, SizeOf(Buffer1), True,
dwNotifyFilter, @Return, @OverLapped, nil) then
begin
//Attente d'une modification
repeat
Waiting := GetOverLappedResult(HandleDirectory, OverLapped, Return, False);
//Création d'un événement pour relancer une nouvelle configuration
if EventThread.WaitFor(0) = wrSignaled then
FDirectoryInProgress := '';
//Pour recevoir les messages du threadfile
Application.ProcessMessages;
until Waiting or(GetLastError <> ERROR_IO_INCOMPLETE) or Terminated or (FDirectoryMonitorSrc <> FDirectoryInProgress);
if Waiting and not Terminated and (FDirectoryMonitorSrc = FDirectoryInProgress) then
begin
ListInfo.Clear;
Action1 := Buffer1.Action;
Str1 := Copy(Buffer1.FileName,0,Buffer1.FileNameLength div 2);
if Action1 = FILE_ACTION_RENAMED_OLD_NAME then
begin
Move(Buffer1.FileName[(Buffer1.NextEntryOffset div 2)-6], Buffer2, SizeOf(Buffer2));
// Action2 := Buffer2.Action;
Str2 := Copy(Buffer2.FileName, 0, Buffer2.FileNameLength div 2);
ListInfo.Add(FormatDateTime('hh:nn:ss:zzz dd/mm/yyy "FILE_ACTION_RENAMED"',Now));
ListInfo.Add('OLD : '+FDirectoryInProgress+'\'+Str1);
ListInfo.Add('NEW : '+FDirectoryInProgress+'\'+Str2);
end;
if Action1 = FILE_ACTION_MODIFIED then
begin
ListInfo.Add(FormatDateTime('hh:nn:ss:zzz dd/mm/yyy "FILE_ACTION_MODIFIED"',Now));
ListInfo.Add('MODIFIED : '+FDirectoryInProgress+'\'+Str1);
end;
if Action1 = FILE_ACTION_REMOVED then
begin
ListInfo.Add(FormatDateTime('hh:nn:ss:zzz dd/mm/yyy "FILE_ACTION_REMOVED"',Now));
ListInfo.Add('REMOVED : '+FDirectoryInProgress+'\'+Str1);
end;
if Action1 = FILE_ACTION_ADDED then
begin
ListInfo.Add(FormatDateTime('hh:nn:ss:zzz dd/mm/yyy "FILE_ACTION_ADDED"',Now));
ListInfo.Add('ADD : '+FDirectoryInProgress+'\'+Str1);
end;
Synchronize(MAJAffichage);
if (FFollowedFile <> -1) and (Action1 = FILE_ACTION_RENAMED_OLD_NAME) then
begin
if FhLogWnd = INVALID_HANDLE_VALUE then
FhLogWnd := AllocateHWnd(WMUserStop);
if ArrayThreadFile = nil then
ArrayThreadFile := TThreadList.Create;
//On ouvre la list de thread en cours
List := ArrayThreadFile.LockList;
NotSur := False;
//On vérifie si le fichier n'est pas déjà suivie
for I := 0 to List.Count - 1 do
begin
ThreadFile := List.Items[I];
if ThreadFile.FFileName = FDirectoryInProgress+'\'+Str2 then
begin
NotSur := True;
Break;
end;
end;
if not NotSur then
begin
ThreadFile := TThreadFile.CreateSurv(FDirectoryInProgress+'\'+Str2, FUpdateTStrings2, FFollowedFile, FTimeWait, FhLogWnd);
ArrayThreadFile.Add(ThreadFile);
end;
ArrayThreadFile.UnlockList;
end;
if (FFollowedFile <> -1) and (Action1 <> FILE_ACTION_RENAMED_OLD_NAME) then
begin
if FhLogWnd = INVALID_HANDLE_VALUE then
FhLogWnd := AllocateHWnd(WMUserStop);
if ArrayThreadFile = nil then
ArrayThreadFile := TThreadList.Create;
List := ArrayThreadFile.LockList;
NotSur := False;
for I := 0 to List.Count - 1 do
begin
ThreadFile := List.Items[I];
if ThreadFile.FFileName = FDirectoryInProgress+'\'+Str1 then
begin
NotSur := True;
Break;
end;
end;
if not NotSur then
begin
ThreadFile := TThreadFile.CreateSurv(FDirectoryInProgress+'\'+Str1, FUpdateTStrings2, FFollowedFile, FTimeWait, FhLogWnd);
ArrayThreadFile.Add(ThreadFile);
end;
ArrayThreadFile.UnlockList;
end;
end;
end;
end;
end;
finally
if HandleDirectory <> INVALID_HANDLE_VALUE then
CloseHandle(HandleDirectory);
HandleDirectory := INVALID_HANDLE_VALUE;
ListInfo.Free;
CloseHandle(OverLapped.hEvent);
end;
end;
procedure TThreadSurveillanceDirectory.MAJAffichage;
begin
if FUpdateTStrings1 <> nil then
begin
FUpdateTStrings1.BeginUpdate;
FUpdateTStrings1.AddStrings(ListInfo);
FUpdateTStrings1.EndUpdate;
end;
end;
end. |
Partager