Delphi: CopyFileEx and Thread

I have a thread and a execution routine (function) inside it (in my thread class).

I am trying to do this inside a thread:

CopyFileEx(pchar(ListToCopy.Strings[Loop]),pchar(TN+ExtractFileName(ListToCopy.Strings[Loop])), @ProgressRoutine, nil, nil, 0); 

But I get an error: "Variable required" (error in this: @ProgressRoutine ). If you make the ProgressRoutine function outside the stream, then everything will be fine.

How to use this function inside a stream?

Thanks.

+8
multithreading delphi
source share
1 answer

When you say “outside the stream” and “inside the stream”, you mean “as an autonomous function” and “as a member of a stream object”? Since, if the function is a member of the object, its signature is different, therefore, it does not correspond to the expected compiler.

The way to resolve this issue is to pass Self to CopyFileEx as the lpData parameter. This gives him a pointer that he will return to the callback. Write your callback as a separate function that interprets the lpData parameter as a reference to a stream object and uses this to call a method in your stream object with the same parameters.

EDIT: A simple example. Let's say that the callback has only two parameters: "value" and "lpData":

 procedure ProgressRoutine(value: integer; lpData: pointer); stdcall; var thread: TMyThreadClass; begin thread := lpData; thread.ProgressRoutine(value); end; procedure TMyThreadClass.ProgressRoutine(value: integer); begin //do something with the value here end; procedure TMyThreadClass.Execute; begin CopyFileEx(pchar(ListToCopy.Strings[Loop]),pchar(TN+ExtractFileName(ListToCopy.Strings[Loop])), @ProgressRoutine, Self, nil, 0); //passing Self to lpData; it will get passed back to the callback end; 
+9
source share

All Articles