Vil gerne have feedback paa min foerste traad.
Hej,Jeg har for foerste gang programmeret en separat traad, og vil gerne have jeres feedback, om jeg nu har gjort alt korrekt :)
Det, den separate traad goer, er at kopiere filer fra A til B maksimum en gang i minuttet.
Naar jeg siger maksimum en gang i minuttet, er det fordi, jeg stopper og starter traaden fra min hovedtraad efter behov.
Her er selve traaden:
unit UnitCopyThread;
interface
uses
Classes, Windows, SysUtils, UnitSettings, Forms;
type
TCopyThread = class(TThread)
private
protected
constructor Create(aSuspended: Bool);
procedure Execute; override;
end;
implementation
procedure TCopyThread.Execute;
var
SearchRec: TSearchRec;
begin
while not Terminated do
begin
if DirectoryExists(ExtractFilePath(Application.ExeName) + 'TempTests') then
begin
FindFirst(ExtractFilePath(Application.ExeName) + 'TempTests\' + '*.xml', faAnyFile+faReadOnly, SearchRec);
if CopyFile(PChar(ExtractFilePath(Application.ExeName) + 'TempTests\' + SearchRec.Name), PChar(frmSettings.PathTest + '\' + SearchRec.Name), True) then
DeleteFile(PChar(ExtractFilePath(Application.ExeName) + 'TempTests\' + SearchRec.Name));
while FindNext(SearchRec) = 0 do
begin
if CopyFile(PChar(ExtractFilePath(Application.ExeName) + 'TempTests\' + SearchRec.Name), PChar(frmSettings.PathTest + '\' + SearchRec.Name), True) then
DeleteFile(PChar(ExtractFilePath(Application.ExeName) + 'TempTests\' + SearchRec.Name));
end;
end;
if DirectoryExists(ExtractFilePath(Application.ExeName) + 'TempBadTests') then
begin
FindFirst(ExtractFilePath(Application.ExeName) + 'TempBadTests\' + '*.xml', faAnyFile+faReadOnly, SearchRec);
if CopyFile(PChar(ExtractFilePath(Application.ExeName) + 'TempBadTests\' + SearchRec.Name), PChar(frmSettings.PathBadTest + '\' + SearchRec.Name), True) then
DeleteFile(PChar(ExtractFilePath(Application.ExeName) + 'TempBadTests\' + SearchRec.Name));
while FindNext(SearchRec) = 0 do
begin
if CopyFile(PChar(ExtractFilePath(Application.ExeName) + 'TempBadTests\' + SearchRec.Name), PChar(frmSettings.PathBadTest + '\' + SearchRec.Name), True) then
DeleteFile(PChar(ExtractFilePath(Application.ExeName) + 'TempBadTests\' + SearchRec.Name));
end;
end;
FindClose(SearchRec);
Sleep(60000); //Run thread maximum every xx ms.
end;
end;
constructor TCopyThread.Create(aSuspended: Bool);
begin
FreeOnTerminate := True;
inherited Create(aSuspended);
end;
end.
Her starter jeg traaden fra min hoved unit (i FormShow event):
CopyThread := TCopyThread.Create(False); //Due to False Execute will be called immediately.
Her "pauser" jeg traaden fra min hoved unit:
CopyThread.Suspend;
Her "genstarter" jeg traaden fra min hoved unit:
CopyThread.Resume;
Her terminerer jeg traaden fra min hoved unit (i OnDestroy event):
CopyThread.Terminate;
Kom med jeres mening!