【问题标题】:Copying files which the main thread adds to a stringlist using a thread使用线程复制主线程添加到字符串列表的文件
【发布时间】:2011-01-06 01:08:36
【问题描述】:

我有一个网站创建程序,它在构建网站时会创建数百个文件。

当互联网根文件夹位于本地电脑上时,程序运行良好。如果 Internet 根文件夹位于网络驱动器上,则复制创建的页面比创建页面本身需要更长的时间(页面的创建已相当优化)。

我正在考虑在本地创建文件,将创建的文件的名称添加到 TStringList 并让另一个线程将它们复制到网络驱动器(从 TStringList 中删除复制的文件)。

但是,我以前从未使用过线程,并且在涉及线程的其他 Delphi 问题中找不到现有答案(如果我们可以在搜索字段中使用 and 运算符),所以我现在想问是否有人有一个可以做到这一点的工作示例(或者可以将我指向一些带有工作 Delphi 代码的文章)?

我正在使用 Delphi 7。

已编辑:我的示例项目(感谢 mghie 的原始代码 - 在此再次感谢他)。

  ...
  fct : TFileCopyThread;
  ...

  procedure TfrmMain.FormCreate(Sender: TObject);
  begin
     if not DirectoryExists(DEST_FOLDER)
     then
        MkDir(DEST_FOLDER);
     fct := TFileCopyThread.Create(Handle, DEST_FOLDER);
  end;


  procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
  begin
     FreeAndNil(fct);
  end;

  procedure TfrmMain.btnOpenClick(Sender: TObject);
  var sDir : string;
      Fldr : TedlFolderRtns;
      i : integer;
  begin
     if PickFolder(sDir,'')
     then begin
        // one of my components, returning a filelist [non threaded  :) ] 
        Fldr := TedlFolderRtns.Create();
        Fldr.FileList(sDir,'*.*',True);
        for i := 0 to Fldr.TotalFileCnt -1 do
        begin
           fct.AddFile( fldr.ResultList[i]);
        end;
     end;
  end;

  procedure TfrmMain.wmFileBeingCopied(var Msg: Tmessage);
  var s : string;
  begin
     s := fct.FileBeingCopied;
     if s <> ''
     then
        lbxFiles.Items.Add(fct.FileBeingCopied);
     lblFileCount.Caption := IntToStr( fct.FileCount );
  end;

和单位

  unit eFileCopyThread;
  interface
  uses
     SysUtils, Classes, SyncObjs, Windows, Messages;
  const
    umFileBeingCopied = WM_USER + 1;
  type

    TFileCopyThread = class(TThread)
    private
      fCS: TCriticalSection;
      fDestDir: string;
      fSrcFiles: TStrings;
      fFilesEvent: TEvent;
      fShutdownEvent: TEvent;
      fFileBeingCopied: string;
      fMainWindowHandle: HWND;
      fFileCount: Integer;
      function GetFileBeingCopied: string;
    protected
      procedure Execute; override;
    public
      constructor Create(const MainWindowHandle:HWND; const ADestDir: string);
      destructor Destroy; override;

      procedure AddFile(const ASrcFileName: string);
      function IsCopyingFiles: boolean;
      property FileBeingCopied: string read GetFileBeingCopied;
      property FileCount: Integer read fFileCount;
    end;

  implementation
  constructor TFileCopyThread.Create(const MainWindowHandle:HWND;const ADestDir: string);
  begin
    inherited Create(True);
    fMainWindowHandle := MainWindowHandle;
    fCS := TCriticalSection.Create;
    fDestDir := IncludeTrailingBackslash(ADestDir);
    fSrcFiles := TStringList.Create; 
    fFilesEvent := TEvent.Create(nil, True, False, ''); 
    fShutdownEvent := TEvent.Create(nil, True, False, ''); 
    Resume; 
  end; 

  destructor TFileCopyThread.Destroy; 
  begin 
    if fShutdownEvent <> nil then 
      fShutdownEvent.SetEvent; 
    Terminate;
    WaitFor;
    FreeAndNil(fFilesEvent);
    FreeAndNil(fShutdownEvent);
    FreeAndNil(fSrcFiles);
    FreeAndNil(fCS);
    inherited;
  end;

  procedure TFileCopyThread.AddFile(const ASrcFileName: string);
  begin
    if ASrcFileName <> ''
    then begin
      fCS.Acquire;
      try
        fSrcFiles.Add(ASrcFileName);
        fFileCount := fSrcFiles.Count;
        fFilesEvent.SetEvent;
      finally
        fCS.Release;
      end;
    end;
  end;

  procedure TFileCopyThread.Execute;
  var
    Handles: array[0..1] of THandle;
    Res: Cardinal;
    SrcFileName, DestFileName: string;
  begin
    Handles[0] := fFilesEvent.Handle;
    Handles[1] := fShutdownEvent.Handle;
    while not Terminated do
    begin
      Res := WaitForMultipleObjects(2, @Handles[0], False, INFINITE);
      if Res = WAIT_OBJECT_0 + 1 then
        break;
      if Res = WAIT_OBJECT_0
      then begin
        while not Terminated do
        begin
          fCS.Acquire;
          try
            if fSrcFiles.Count > 0
            then begin
              SrcFileName := fSrcFiles[0];
              fSrcFiles.Delete(0);
              fFileCount := fSrcFiles.Count;
              PostMessage( fMainWindowHandle,umFileBeingCopied,0,0 );
           end else
               SrcFileName := '';
           fFileBeingCopied := SrcFileName;
            if SrcFileName = '' then
              fFilesEvent.ResetEvent;
          finally
            fCS.Release;
          end;

          if SrcFileName = '' then
            break;
          DestFileName := fDestDir + ExtractFileName(SrcFileName);
          CopyFile(PChar(SrcFileName), PChar(DestFileName), True);
        end;
      end;
    end;
  end;

  function TFileCopyThread.IsCopyingFiles: boolean;
  begin 
    fCS.Acquire; 
    try 
      Result := (fSrcFiles.Count > 0) 
        // last file is still being copied 
        or (WaitForSingleObject(fFilesEvent.Handle, 0) = WAIT_OBJECT_0); 
    finally 
      fCS.Release; 
    end; 
  end; 

  // new version - edited after receiving comments 
  function TFileCopyThread.GetFileBeingCopied: string; 
  begin 
     fCS.Acquire; 
     try 
        Result := fFileBeingCopied; 
     finally 
        fCS.Release; 
     end; 
  end; 

  // old version - deleted after receiving comments 
  //function TFileCopyThread.GetFileBeingCopied: string;
  //begin
  //  Result := '';
  //  if fFileBeingCopied <> ''
  //  then begin
  //    fCS.Acquire;
  //    try
  //      Result := fFileBeingCopied;
  //      fFilesEvent.SetEvent;
  //    finally
  //      fCS.Release;
  //    end;
  //  end;
  //end;

  end.

我们将不胜感激。

阅读 cmets 并查看示例,您会发现解决方案的不同方法,所有这些方法都有优缺点。

当尝试实现一个复杂的新功能(就像线程对我而言)时,问题是你几乎总能找到一些似乎工作的东西......起初。直到后来你才发现事情应该以不同的方式做的艰难。线程就是一个很好的例子。

StackOverflow 之类的网站非常棒。多么棒的社区。​​p>

【问题讨论】:

    标签: multithreading delphi delphi-7 file-copying


    【解决方案1】:

    快速而肮脏的解决方案:

    type
      TFileCopyThread = class(TThread)
      private
        fCS: TCriticalSection;
        fDestDir: string;
        fSrcFiles: TStrings;
        fFilesEvent: TEvent;
        fShutdownEvent: TEvent;
      protected
        procedure Execute; override;
      public
        constructor Create(const ADestDir: string);
        destructor Destroy; override;
    
        procedure AddFile(const ASrcFileName: string);
        function IsCopyingFiles: boolean;
      end;
    
    constructor TFileCopyThread.Create(const ADestDir: string);
    begin
      inherited Create(True);
      fCS := TCriticalSection.Create;
      fDestDir := IncludeTrailingBackslash(ADestDir);
      fSrcFiles := TStringList.Create;
      fFilesEvent := TEvent.Create(nil, True, False, '');
      fShutdownEvent := TEvent.Create(nil, True, False, '');
      Resume;
    end;
    
    destructor TFileCopyThread.Destroy;
    begin
      if fShutdownEvent <> nil then
        fShutdownEvent.SetEvent;
      Terminate;
      WaitFor;
      FreeAndNil(fFilesEvent);
      FreeAndNil(fShutdownEvent);
      FreeAndNil(fSrcFiles);
      FreeAndNil(fCS);
      inherited;
    end;
    
    procedure TFileCopyThread.AddFile(const ASrcFileName: string);
    begin
      if ASrcFileName <> '' then begin
        fCS.Acquire;
        try
          fSrcFiles.Add(ASrcFileName);
          fFilesEvent.SetEvent;
        finally
          fCS.Release;
        end;
      end;
    end;
    
    procedure TFileCopyThread.Execute;
    var
      Handles: array[0..1] of THandle;
      Res: Cardinal;
      SrcFileName, DestFileName: string;
    begin
      Handles[0] := fFilesEvent.Handle;
      Handles[1] := fShutdownEvent.Handle;
      while not Terminated do begin
        Res := WaitForMultipleObjects(2, @Handles[0], False, INFINITE);
        if Res = WAIT_OBJECT_0 + 1 then
          break;
        if Res = WAIT_OBJECT_0 then begin
          while not Terminated do begin
            fCS.Acquire;
            try
              if fSrcFiles.Count > 0 then begin
                SrcFileName := fSrcFiles[0];
                fSrcFiles.Delete(0);
              end else
                SrcFileName := '';
              if SrcFileName = '' then
                fFilesEvent.ResetEvent;
            finally
              fCS.Release;
            end;
    
            if SrcFileName = '' then
              break;
            DestFileName := fDestDir + ExtractFileName(SrcFileName);
            CopyFile(PChar(SrcFileName), PChar(DestFileName), True);
          end;
        end;
      end;
    end;
    
    function TFileCopyThread.IsCopyingFiles: boolean;
    begin
      fCS.Acquire;
      try
        Result := (fSrcFiles.Count > 0)
          // last file is still being copied
          or (WaitForSingleObject(fFilesEvent.Handle, 0) = WAIT_OBJECT_0);
      finally
        fCS.Release;
      end;
    end;
    

    要在生产代码中使用它,您需要添加错误处理,可能需要一些进度通知,并且复制本身可能应该以不同的方式实现,但这应该可以帮助您入门。

    回答您的问题:

    我应该在主程序的 FormCreate 中创建 FileCopyThread(并让它运行),这会以某种方式减慢程序吗?

    您可以创建线程,它会阻塞事件并消耗 0 个 CPU 周期,直到您添加要复制的文件。复制完所有文件后,线程将再次阻塞,因此在程序的整个运行时保持它除了消耗一些内存之外没有负面影响。

    我可以向 FileCopyThread 添加正常的事件通知吗(这样我就可以发送一个事件,如属性 onProgress:TProgressEvent 读取 fOnProgressEvent 写入 fOnProgressEvent; fi 列表中的当前文件数和当前处理的文件。我会喜欢在添加复制例程之前和之后调用这个

    您可以添加通知,但要使它们真正有用,它们需要在主线程的上下文中执行。最简单和最丑陋的方法是用Synchronize() 方法包装它们。查看 Delphi Threads 演示,了解如何执行此操作的示例。然后阅读通过在 SO 上搜索“[delphi] 同步”找到的一些问题和答案,看看这种技术有很多缺点。

    但是,我不会以这种方式实现通知。如果您只想显示进度,则无需使用每个文件进行更新。此外,您已经在 VCL 线程中拥有所有必要的信息,在您添加要复制的文件的位置。您可以简单地使用 100 的Interval 启动一个计时器,并让计时器事件处理程序检查线程是否仍处于忙碌状态,以及还有多少文件需要复制。当线程再次被阻塞时,您可以禁用计时器。如果您需要来自线程的更多或不同的信息,那么您可以轻松地向线程类添加更多线程安全的方法(例如返回待处理文件的数量)。我从一个最小的界面开始,以使事情变得小而简单,仅将其用作灵感。

    评论您更新的问题:

    你有这个代码:

    function TFileCopyThread.GetFileBeingCopied: string;
    begin
      Result := '';
      if fFileBeingCopied <> '' then begin
        fCS.Acquire;
        try
          Result := fFileBeingCopied;
          fFilesEvent.SetEvent;
        finally
          fCS.Release;
        end;
      end;
    end;
    

    但它有两个问题。首先,所有对数据字段的访问都需要受到保护以确保安全,然后您只是在读取数据,而不是添加新文件,因此无需设置事件。修改后的方法就是:

    function TFileCopyThread.GetFileBeingCopied: string;
    begin
      fCS.Acquire;
      try
        Result := fFileBeingCopied;
      finally
        fCS.Release;
      end;
    end;
    

    此外,您只设置了fFileBeingCopied 字段,但从不重置它,因此它始终等于最后复制的文件,即使线程被阻塞。当最后一个文件被复制时,您应该将该字符串设置为空,当然,在获取关键部分时这样做。只需将作业移过if 块即可。

    【讨论】:

    • 感谢您的回答..我会尝试一下并告诉您结果。
    • +1:第一个测试程序有效。但是还有两个问题:1/我应该在主程序的 FormCreate 中创建 FileCopyThread(并让它运行),这会以某种方式减慢程序吗?和 2/ 我可以向 FileCopyThread 添加正常的事件通知(这样我就可以像property onProgress:TProgressEvent read fOnProgressEvent write fOnProgressEvent; 一样发送一个事件,其中包含列表中的当前文件数和当前处理的文件。我想在添加时调用它以及复制程序之前和之后。
    • 我找不到的示例似乎都使用了 Delphi 事件,所以我认为这是不可能的。我目前正在使用 PostMessage。如果有任何其他想法,我将不胜感激。但请在我之前的评论中回答第 1 点/。谢谢
    • @Edelcom:您可以在应用程序启动时或以后创建线程,没关系。该线程不会浪费一个CPU周期,除非复制文件,否则它会完全阻塞。但是,我会在需要时创建它,即当需要复制第一个文件时 - 这样您的应用程序将启动得更快。但这主要是风格问题。关于实施事件,您可能应该提出一个新问题,以使事情更加集中。
    • @Edelcom:不,只有可以从多个线程同时访问的数据才需要被同步对象保护。 Result 仅在当前线程的上下文中有效,因此不可能进行并发访问。但是,作业有阅读和写作部分,如果需要保护其中之一,则必须保护整个作业。我真的建议阅读 Harvey 的文章 (eonclash.com/Tutorials/Multithreading/MartinHarvey1.1/ToC.html),只有充分了解基础知识才能成功使用多线程。
    【解决方案2】:

    如果您有点不愿意像在mghie solution 中那样直接使用TThread 并直接处理TThread,那么另一种可能更快的方法是使用Andreas Hausladen's AsyncCalls

    骨架代码:

    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;
    

    【讨论】:

    • 我决定不使用 AsyncCalls,因为 I/O 应该序列化以最大化带宽,而调度多个线程实际上会适得其反。除非文件被移动到不同的目的地,在这种情况下,多线程可以改善事情。
    • +1:我会去尝试线程解决方案(也是因为我以前从未使用过线程,这似乎是尝试它们的好理由),但感谢您提供替代解决方案。我也会尝试这个解决方案(只是看看它是如何工作的)。我正在将文件复制到网络上的不同文件夹,尽管不是随机的(首先是英语网页,然后是荷兰语网页,然后是图像,等等......你得到了图片)。
    • @mghie。您可以设置 ThreadPool 中的线程数以满足您的需求和环境。
    【解决方案3】:

    使用线程的一个好的开始是 Delphi 在the Delphi about site

    为了使您的解决方案有效,您需要一个工作线程的作业队列。可以使用字符串列表。但是在任何情况下,您都需要保护队列,以便在任何时候只有一个线程可以写入它。即使写线程被挂起。

    您的应用程序写入队列。所以必须有一个受保护的写方法。

    您的线程从队列中读取和删除。所以必须有一个受保护的读取/删除方法。

    您可以使用临界区来确保在任何时候只有其中一个可以访问队列。

    【讨论】:

    • 感谢您的回答,但我真的希望有一个可行的例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多