Delphi:CopyFileEx 和线程
我里面有一个线程和一个进度例程(一个函数)(在我的线程类中)。
我尝试在线程内执行此操作:
CopyFileEx(pchar(ListToCopy.Strings[Loop]),pchar(TN+ExtractFileName(ListToCopy.Strings[Loop])), @ProgressRoutine, nil, nil, 0);
但出现错误:“需要变量”(错误在此:@ProgressRoutine)。如果将ProgressRoutine函数放在线程之外则一切正常。
如何在线程内使用该函数?
谢谢。
I have a thread and a Progress Routine (a function) inside it (in my thread class).
I try to do this inside the thread:
CopyFileEx(pchar(ListToCopy.Strings[Loop]),pchar(TN+ExtractFileName(ListToCopy.Strings[Loop])), @ProgressRoutine, nil, nil, 0);
But I get an error: "Variable required" (the error is in this: @ProgressRoutine). If to make the function ProgressRoutine outside the thread then all will be normal.
How to use that function inside thread?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您说“线程外部”和“线程内部”时,您的意思是“作为独立函数”和“作为线程对象的成员”吗?因为如果一个函数是一个对象的成员,它的签名是不同的,所以它与编译器期望的不匹配。
解决此问题的方法是将
Self
作为lpData
参数传递给CopyFileEx
。这给它一个将传递回回调的指针。将回调编写为独立函数,该函数将lpData
参数解释为线程对象引用,并使用它使用相同的参数调用线程对象上的方法。编辑:简单的例子。假设回调只有两个参数,称为“value”和“lpData”:
When you say "outside the thread" and "inside the thread", do you mean "as a standalone function" and "as a member of the thread object"? Because if a function is a member of an object, its signature is different, so it doesn't match what the compiler is expecting.
The way you can resolve this is to pass
Self
toCopyFileEx
as thelpData
parameter. This gives it a pointer that it will pass back to the callback. Write your callback as a standalone function that interprets thelpData
parameter as a thread object reference and uses that to call the method on your thread object with the same parameters.EDIT: Simple example. Let's say that the callback only has two parameters, called "value" and "lpData":