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 usualTThread.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 toCopyFileEx.You can give the callback function access to the associated thread object with the
lpDataparameter. Pass a reference to the thread object for that parameter when you callCopyFileEx: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
lpDataparameter because that’s going to be passed implicitly as theSelfparameter.