Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can CopyFileEx be called from a secondary thread?

Is it possible and right to call CopyFileEx and CopyCallback/ProgressRoutine function (ProgressBar.Position will be synchronized) from a thread?

Can I declare CopyCallback/ProgressRoutine function in a thread? I get Error: "Variable required" in CopyFileEx on @ProgressRoutine.

like image 735
maxfax Avatar asked Jun 16 '11 03:06

maxfax


1 Answers

Of course it's possible. The callback function will be called in the context of the thread that calls CopyFileEx. If you need to synchronize some UI commands, use Delphi's usual TThread.Synchronize, or whatever other inter-thread synchronization techniques you want.

The callback function cannot be a method of the thread class. It needs to match the signature dictated by the API, so it needs to be a standalone function. When you've declared it correctly, you won't need to use the @ operator when you pass it to CopyFileEx.

function CopyProgressRoutine(TotalFileSize, TotalBytesTransferred: Int64;
  StreamSize, StreamBytesTransferred: Int64;
  dwStreamNumber, dwCallbackReason: DWord;
  hSourceFile, hDestinationFile: THandle;
  lpData: Pointer): DWord; stdcall;

You can give the callback function access to the associated thread object with the lpData parameter. Pass a reference to the thread object for that parameter when you call CopyFileEx:

procedure TCopyThread.Execute;
begin
  ...
  CopyResult := CopyFileEx(CurrentName, NewName, CopyProgressRoutine, Self,
    @Cancel, CopyFlags);
  ...
end;

With access to the thread object, you can call methods on that object, including its own progress routine, so the following could constitute the entirety of the standalone function. It can delegate everything else back to a method of your object. Here I've assumed the method has all the same parameters as the standalone function, except that it omits the lpData parameter because that's going to be passed implicitly as the Self parameter.

function CopyProgressRoutine;
var
  CopyThread: TCopyThread;
begin
  CopyThread := lpData;
  Result := CopyThread.ProgressRoutine(TotalSize, TotalBytesTransferred,
    StreamSize, StreamBytesTransferred, dwStreamNumber,
    dwCallbackReason, hSourceFile, hDestinationFile);
end;
like image 161
Rob Kennedy Avatar answered Sep 19 '22 15:09

Rob Kennedy