Delphi:CopyFileEx 和线程

发布于 2024-11-28 16:54:51 字数 341 浏览 3 评论 0原文

我里面有一个线程和一个进度例程(一个函数)(在我的线程类中)。

我尝试在线程内执行此操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヤ经典坏疍 2024-12-05 16:54:51

当您说“线程外部”和“线程内部”时,您的意思是“作为独立函数”和“作为线程对象的成员”吗?因为如果一个函数是一个对象的成员,它的签名是不同的,所以它与编译器期望的不匹配。

解决此问题的方法是将 Self 作为 lpData 参数传递给 CopyFileEx。这给它一个将传递回回调的指针。将回调编写为独立函数,该函数将 lpData 参数解释为线程对象引用,并使用它使用相同的参数调用线程对象上的方法。

编辑:简单的例子。假设回调只有两个参数,称为“value”和“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;

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 to CopyFileEx as the lpData parameter. This gives it a pointer that it will pass back to the callback. Write your callback as a standalone function that interprets the lpData 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":

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文