|
92 | 92 |
|
93 | 93 | TWorkThread = Class(TThread) |
94 | 94 | private |
| 95 | + fBusy: integer; // 0 = nichts mehr zu tun, > 0 Wir arbeiten noch irgendwas.. |
95 | 96 | fAJob: TJob; // Der Job der Im Moment Abgearbeitet wird. |
96 | 97 | fJobProgress: Byte; // Fortschritt in Prozent füf den Aktuellen Job |
97 | 98 | fStatistic: TTransfereStatistic; |
|
146 | 147 | Procedure AddJob(Const Job: TJob); |
147 | 148 | Function ExistJob(Const Job: TJob): Boolean; |
148 | 149 |
|
149 | | - Function JobsPending(): Boolean; // True, wenn der Worker noch irgendwas zu tun hat... |
| 150 | + Function Busy: Boolean; // True, wenn der Worker noch irgendwas zu tun hat... |
150 | 151 |
|
151 | 152 | Procedure CancelAllJobs(); |
152 | 153 |
|
|
533 | 534 | BufferSize: Integer; |
534 | 535 | FreeDiskSpace: Int64; |
535 | 536 | Begin |
| 537 | + inc(fBusy); |
536 | 538 | fCopyFileDetailError := 'Unknown error'; |
537 | 539 | Result := False; |
538 | 540 | If FCancelallJobs Or fCancelActualJob Or (source = dest) Then Begin |
539 | 541 | fCopyFileDetailError := ''; |
540 | 542 | result := true; |
| 543 | + dec(fBusy); |
541 | 544 | exit; |
542 | 545 | End; |
543 | 546 | // Sicherstellen das dest nicht existiert, Die Fälle wo es existieren darf und |
544 | 547 | // Nicht "überschrieben" werden soll klärt der Aufrufer!. |
545 | 548 | If FileExistsUTF8(dest) Then Begin |
546 | 549 | If Not DeleteFileUTF8(dest) Then Begin |
547 | 550 | fCopyFileDetailError := 'Could not delete dest file'; |
| 551 | + dec(fBusy); |
548 | 552 | exit; |
549 | 553 | End; |
550 | 554 | End; |
551 | 555 | Try |
552 | 556 | SourceFile := FileOpen(utf8tosys(source), fmOpenRead); |
553 | 557 | If SourceFile = 0 Then Begin |
554 | 558 | fCopyFileDetailError := 'Unable to read source file'; |
| 559 | + dec(fBusy); |
555 | 560 | exit; |
556 | 561 | End; |
557 | 562 | (* |
|
564 | 569 | FreeDiskSpace := GetFreeDiskSpaceOf(ExtractFilePath(dest)); |
565 | 570 | If FreeDiskSpace <= RemainingFileSize Then Begin |
566 | 571 | fCopyFileDetailError := 'Not enough diskspace at destination available'; |
| 572 | + dec(fBusy); |
567 | 573 | exit; |
568 | 574 | End; |
569 | 575 | DestFile := FileCreate(utf8tosys(dest)); |
570 | 576 | If DestFile = 0 Then Begin |
571 | 577 | fCopyFileDetailError := 'Unable to create destination file'; |
572 | 578 | FileClose(SourceFile); |
| 579 | + dec(fBusy); |
573 | 580 | exit; |
574 | 581 | End; |
575 | 582 | FileSeek(SourceFile, 0, fsFromBeginning); |
|
580 | 587 | FileClose(SourceFile); |
581 | 588 | fCopyFileDetailError := ''; |
582 | 589 | result := true; // Das Stimmt zwar nicht, erzeugt aber im Abgang die wenigsten Fehler, Störungen |
| 590 | + dec(fBusy); |
583 | 591 | exit; |
584 | 592 | End; |
585 | 593 | If JobPause Then Begin |
|
593 | 601 | FileClose(SourceFile); |
594 | 602 | FileClose(DestFile); |
595 | 603 | fCopyFileDetailError := 'Sourcefile read error'; |
| 604 | + dec(fBusy); |
596 | 605 | exit; |
597 | 606 | End; |
598 | 607 | If FileWrite(DestFile, buffer, BufferSize) = -1 Then Begin |
599 | 608 | // Irgendwas hat nen Fehler ausgelöst |
600 | 609 | FileClose(SourceFile); |
601 | 610 | FileClose(DestFile); |
602 | 611 | fCopyFileDetailError := 'Destfile write error'; |
| 612 | + dec(fBusy); |
603 | 613 | exit; |
604 | 614 | End; |
605 | 615 | // Und noch die Statistik Nach ziehen |
|
625 | 635 | // KA was ich da noch Vergessen habe |
626 | 636 | On AV: Exception Do Begin |
627 | 637 | fCopyFileDetailError := av.Message; |
| 638 | + dec(fBusy); |
628 | 639 | exit; |
629 | 640 | End; |
630 | 641 | End; |
631 | 642 | fCopyFileDetailError := ''; |
| 643 | + dec(fBusy); |
632 | 644 | Result := True; |
633 | 645 | End; |
634 | 646 |
|
|
666 | 678 |
|
667 | 679 | Procedure TWorkThread.Init; |
668 | 680 | Begin |
| 681 | + fBusy := 0; |
669 | 682 | fLCLSubJobArray := Nil; |
670 | 683 | fAllResult := jaNotChoosen; |
671 | 684 | FCancelallJobs := false; |
|
831 | 844 | Begin |
832 | 845 | result := false; |
833 | 846 | If Not assigned(fAJob) Then exit; |
| 847 | + inc(fBusy); |
834 | 848 | If FCancelallJobs Or fCancelActualJob Then Begin |
835 | 849 | // Beim Abcanceln der Jobs rufen wir den FinishJob dennoch auf, damit die LCL die Chance hat diesen aus ihren Listen zu streichen.. |
836 | 850 | If Assigned(OnFinishJob) Then Begin |
837 | 851 | Synchronize(@LCLOnFinishJob); |
838 | 852 | End; |
839 | 853 | result := true; |
| 854 | + dec(fbusy); |
840 | 855 | exit; |
841 | 856 | End; |
842 | 857 | If Assigned(OnStartJob) Then Begin |
|
848 | 863 | jtMoveFile, jtCopyFile: Begin |
849 | 864 | If Not FileExistsUTF8(fAJob.Source) Then Begin |
850 | 865 | AddToErrorLog('Source file does not exist anymore.'); |
| 866 | + dec(fBusy); |
851 | 867 | exit; |
852 | 868 | End; |
853 | 869 | // Ziel Verzeichnis erstellen |
854 | 870 | If Not ForceDirectoriesUTF8(ExtractFileDir(fAJob.Dest)) Then Begin |
855 | 871 | AddToErrorLog('Unable to create destination folder.'); |
| 872 | + dec(fBusy); |
856 | 873 | exit; |
857 | 874 | End; |
858 | 875 | If FileExistsUTF8(fAJob.Dest) Then Begin |
|
864 | 881 | Synchronize(@LCLOnFinishJob); |
865 | 882 | End; |
866 | 883 | result := true; |
| 884 | + dec(fBusy); |
867 | 885 | exit; |
868 | 886 | End |
869 | 887 | Else Begin |
|
878 | 896 | Synchronize(@LCLOnFinishJob); |
879 | 897 | End; |
880 | 898 | FQuestionJobFifo.Push(fAJob); |
| 899 | + dec(fBusy); |
881 | 900 | exit; |
882 | 901 | End; |
883 | 902 | jaSkip: Begin |
|
886 | 905 | Synchronize(@LCLOnFinishJob); |
887 | 906 | End; |
888 | 907 | result := true; |
| 908 | + dec(fBusy); |
889 | 909 | exit; |
890 | 910 | End; |
891 | 911 | jaReplace: Begin |
892 | 912 | If Not DeleteFileUTF8(fAJob.Dest) Then Begin |
893 | 913 | AddToErrorLog('Unable to delete destination file.'); |
| 914 | + dec(fBusy); |
894 | 915 | exit; |
895 | 916 | End; |
896 | 917 | End; |
|
899 | 920 | End; |
900 | 921 | If Not FileCopy(fAJob.Source, fAJob.Dest) Then Begin |
901 | 922 | AddToErrorLog('Unable to copy file to destination folder: ' + fCopyFileDetailError); |
| 923 | + dec(fBusy); |
902 | 924 | exit; |
903 | 925 | End; |
904 | 926 | If fAJob.JobType = jtMoveFile Then Begin |
905 | 927 | If Not DeleteFileUTF8(fAJob.Source) Then Begin |
906 | 928 | AddToErrorLog('Unable to delete file.'); |
| 929 | + dec(fBusy); |
907 | 930 | exit; |
908 | 931 | End; |
909 | 932 | End; |
910 | 933 | End; |
911 | 934 | jtMoveDir, jtCopyDir: Begin |
912 | 935 | If Not DirectoryExistsUTF8(fAJob.Source) Then Begin |
913 | 936 | AddToErrorLog('Source directory does not exist anymore.'); |
| 937 | + dec(fBusy); |
914 | 938 | exit; |
915 | 939 | End; |
916 | 940 | // Ziel Ordner Erstellen |
917 | 941 | s := extractfilename(ExcludeTrailingPathDelimiter(fAJob.Source)); |
918 | 942 | If Not ForceDirectoriesUTF8(IncludeTrailingPathDelimiter(fAJob.Dest) + s) Then Begin |
919 | 943 | AddToErrorLog('Unable to create dest dir.'); |
| 944 | + dec(fBusy); |
920 | 945 | exit; |
921 | 946 | End; |
922 | 947 | // Rekursiv Verschieben / Kopieren |
|
949 | 974 | jtDelFile: Begin |
950 | 975 | If Not DeleteFileUTF8(fAJob.Source) Then Begin |
951 | 976 | AddToErrorLog('Unable to delete file.'); |
| 977 | + dec(fBusy); |
952 | 978 | exit; |
953 | 979 | End; |
954 | 980 | End; |
|
967 | 993 | jtDelDir: Begin |
968 | 994 | If Not DelFolder(fAJob.Source) Then Begin |
969 | 995 | AddToErrorLog('Unable to delete folder.'); |
| 996 | + dec(fBusy); |
970 | 997 | exit; |
971 | 998 | End; |
972 | 999 | End; |
|
975 | 1002 | Synchronize(@LCLOnFinishJob); |
976 | 1003 | End; |
977 | 1004 | result := true; |
| 1005 | + dec(fbusy); |
978 | 1006 | End; |
979 | 1007 |
|
980 | 1008 | Procedure TWorkThread.CheckForOnFileTransfereStatistic; |
|
1149 | 1177 | result := FInJobFifo.ContainsElement(job, @CompareJobs); |
1150 | 1178 | End; |
1151 | 1179 |
|
1152 | | -Function TWorkThread.JobsPending: Boolean; |
| 1180 | +Function TWorkThread.Busy: Boolean; |
1153 | 1181 | Begin |
1154 | | - result := getPendingJobs() <> 0; |
| 1182 | + result := (getPendingJobs() <> 0) Or (fBusy > 0); |
1155 | 1183 | End; |
1156 | 1184 |
|
1157 | 1185 | Procedure TWorkThread.CancelAllJobs; |
|
0 commit comments