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;
TProgressBar.Position
does not requireTThread.Synchronize
IMO.TProgressBar.SetPosition
method never allocates control's handle and works throughSendMessage
call which switches thread context by itself. – Phelloderm