Can CopyFileEx be called from a secondary thread?
Asked Answered
A

1

7

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.

Adorl answered 16/6, 2011 at 3:59 Comment(0)
L
13

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;
Liability answered 16/6, 2011 at 4:28 Comment(4)
Setting TProgressBar.Position does not require TThread.Synchronize IMO. TProgressBar.SetPosition method never allocates control's handle and works through SendMessage call which switches thread context by itself.Phelloderm
Technically, @Serg, there's a race condition. TProgressBar checks HandleAllocated before reading the Handle property. If the handle was already allocated, but it gets destroyed before reading Handle, then the handle will be re-allocated in the wrong thread. That's unlikely to happen, so TProgressBar is probably safe. In general, though, UI updates should get synchronized with the UI thread.Liability
Can I declare CopyCallback/ProgressRoutine function in a thread? I get Error: "Variable required" in CopyFileEx on @ProgressRoutine.Adorl
You could queue instead of fully-locked synchronizing with TThread.Synchronize, though.As

© 2022 - 2024 — McMap. All rights reserved.