Copying files which the main thread adds to a stringlist using a thread

后端 未结 3 2008
不思量自难忘°
不思量自难忘° 2021-02-04 22:12

I have a web creation program which, when building a site, creates hundreds of files.

When the internet root folder is situated on the local pc, the program runs fine.

3条回答
  •  清歌不尽
    2021-02-04 22:57

    If you're somewhat reluctant to go down to the metal and deal with TThread directly like in mghie solution, an alternative, maybe quicker, is to use Andreas Hausladen's AsyncCalls.

    skeleton code:

    procedure MoveFile(AFileName: TFileName; const DestFolder: string);
    //------------------------------------------------------------------------------
    begin
      if DestFolder > '' then
        if CopyFile(PChar(AFileName), PChar(IncludeTrailingPathDelimiter(DestFolder) + ExtractFileName(AFileName)), False) then
          SysUtils.DeleteFile(AFileName)
        else
          RaiseLastOSError;
    end;
    
    procedure DoExport;
    //------------------------------------------------------------------------------
    var
      TempPath, TempFileName: TFileName;
      I: Integer;
      AsyncCallsList: array of IAsyncCall;
    begin
      // find Windows temp directory
      SetLength(TempPath, MAX_PATH);
      SetLength(TempPath, GetTempPath(MAX_PATH, PChar(TempPath)));
    
      // we suppose you have an array of items (1 per file to be created) with some info
      SetLength(AsyncCallsList, Length(AnItemListArray));
      for I := Low(AnItemListArray) to High(AnItemListArray) do
      begin
        AnItem := AnItemListArray[I];
        LogMessage('.Processing current file for '+ AnItem.NAME);
        TempFileName := TempPath + Format(AFormatString, [AnItem.NAME, ...]);
        CreateYourFile(TempFileName);
        LogMessage('.File generated for '+ AnItem.NAME);
        // Move the file to Dest asynchronously, without waiting
        AsyncCallsList[I] := AsyncCall(@MoveFile, [TempFileName, AnItem.DestFolder])
      end;
    
      // final rendez-vous synchronization
      AsyncMultiSync(AsyncCallsList);
      LogMessage('Job finished... ');
    end;
    

提交回复
热议问题