开发者

Can CopyFileEx be called from a secondary thread?

开发者 https://www.devze.com 2023-03-13 19:11 出处:网络
Is it possible and right to call CopyFileEx and CopyCallback/ProgressRoutine function (ProgressBar.Position will be synchronized)from a 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.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号