Delphi,通过 BeginThread 传递指针

发布于 2024-09-30 20:40:04 字数 210 浏览 3 评论 0原文

我正在使用 BeginThread 创建一个线程。

在我用来启动线程的过程中,我想传递一个指向布尔变量的指针,以便分叉线程和主线程都可以将其作为控制变量访问,以告诉一个线程何时完成。

由于开始线程接受参数指针,我尝试传入 Addr(MyPointerVar) 但出现错误。

但我必须逃跑,所以今晚我无法在这里完成我的想法。但如果有人对此有任何想法,我很感激。

I am creating a thread using BeginThread.

In the procedure I am using to start the thread I want to pass a pointer to a boolean variable so that both the forked thread and main thread can access it as a control variable to tell one when the other is done.

Since begin thread takes in a pointer for the parameters i have tried to pass in the Addr(MyPointerVar) but I am getting errors.

But I have to run so I cannot finish my thoughts here tonight. But if anyone has any ideas on doing this I appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

左耳近心 2024-10-07 20:40:04

使用“@”地址运算符将变量的地址传递给 BeginThread(),例如:

var
  ThreadDone: Boolean;
  ThreadId: LongWord;
  ThreadHandle: Integer;

function ThreadFunc(PThreadDone: PBoolean): Integer;
begin
  ...
  PThreadDone^ := True;
  Result := 0;
end;

...

ThreadHandle := BeginThread(nil, 0, @ThreadFunc, @ThreadDone, 0, ThreadId);

话虽如此,主线程在不使用单独变量的情况下检查线程是否完成的另一种方法是传递 BeginThread 返回的线程句柄() 到 WaitForSingleObject() 并查看它是否返回 WAIT_OBJECT_0 :

var
  ThreadId: LongWord;
  ThreadHandle: Integer;

function ThreadFunc(Parameter: Pointer): Integer;
begin
  ...
  Result := 0;
end;

...

ThreadHandle := BeginThread(nil, 0, @ThreadFunc, nil, 0, ThreadId);
...
if WaitForSingleObject(THandle(ThreadHandle), 0) = WAIT_OBJECT_0 then
  finished...
else
  still running...

Use the '@' address operator to pass the variable's address to BeginThread(), eg:

var
  ThreadDone: Boolean;
  ThreadId: LongWord;
  ThreadHandle: Integer;

function ThreadFunc(PThreadDone: PBoolean): Integer;
begin
  ...
  PThreadDone^ := True;
  Result := 0;
end;

...

ThreadHandle := BeginThread(nil, 0, @ThreadFunc, @ThreadDone, 0, ThreadId);

With that said, another way for the main thread to check if the thread is done without using a separate variable is to pass the thread handle returned by BeginThread() to WaitForSingleObject() and see if it returns WAIT_OBJECT_0 or not:

var
  ThreadId: LongWord;
  ThreadHandle: Integer;

function ThreadFunc(Parameter: Pointer): Integer;
begin
  ...
  Result := 0;
end;

...

ThreadHandle := BeginThread(nil, 0, @ThreadFunc, nil, 0, ThreadId);
...
if WaitForSingleObject(THandle(ThreadHandle), 0) = WAIT_OBJECT_0 then
  finished...
else
  still running...
站稳脚跟 2024-10-07 20:40:04

雷米上面的答案是正确的解决方案。

我正在按照雷米上面的建议进行操作,但仍然遇到问题,通过快速测试,我刚刚做了似乎我的错误是在与调用 beginthread 的对象不同的对象中处理线程过程。

Remy's answer above is the correct solution.

I was doing what Remy has suggested above and was still having issues and with a quick test I just did it seems my fault was something dealing with having the threaded procedure in a different object than where beginthread was being called.

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