1

在过程 TdwsFileNotifier.Execute 中,当 ReadDirectoryChanges 调用失败时,通过调用 Terminate 来终止线程,但不会立即退出循环。IMO 应该在终止后立即休息,如下所示:

   FActive:=True;   
   NameThreadForDebugging('FileNotifier '+AnsiString(FDirectory));
   while not Terminated do begin      
       GetQueuedCompletionStatus(FChangeCP, numBytes, completionKey, FPOverlapped, INFINITE);

       if completionKey<>0 then begin
           if not ReadDirectoryChanges(FDirectoryHandle, @FNotificationBuffer,
                                 SizeOf(TdwsFileNotifierBuffer),
                                 (FMode=dnoDirectoryAndSubTree), FNotifyFilter,
                                 @FBytesWritten, @FOverlapped, nil) then
               Terminate;

     fileOpNotification:=@FNotificationBuffer;
     repeat
        offset:=fileOpNotification^.NextEntryOffset;
        if Assigned(FOnFileChanged) then begin
           notify:=True;
           for i:=0 to High(FIgnoredPaths) do begin
              if StrLComp(@fileOpNotification^.FileName[0],
                          PWideChar(Pointer(FIgnoredPaths[i])),
                          Length(FIgnoredPaths[i]))=0 then begin
                 notify:=False;
                 Break;
              end;
           end;
           if notify then begin
              SetString(fileName, fileOpNotification^.FileName,
                        fileOpNotification^.FileNameLength div SizeOf(Char));
              FQueue.QueueNotification(fileName, fileOpNotification^.Action);
           end;
        end;
        fileOpNotification:=@PAnsiChar(fileOpNotification)[offset];
     until offset=0;
     FBytesWritten:=0;
     FillChar(FNotificationBuffer, 0, SizeOf(TdwsFileNotifierBuffer));

   end else Terminate;
   end;
   FActive:=False;
4

0 回答 0