Delphi,通过 BeginThread 传递指针
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用“@”地址运算符将变量的地址传递给 BeginThread(),例如:
话虽如此,主线程在不使用单独变量的情况下检查线程是否完成的另一种方法是传递 BeginThread 返回的线程句柄() 到 WaitForSingleObject() 并查看它是否返回 WAIT_OBJECT_0 :
Use the '@' address operator to pass the variable's address to BeginThread(), eg:
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:
雷米上面的答案是正确的解决方案。
我正在按照雷米上面的建议进行操作,但仍然遇到问题,通过快速测试,我刚刚做了似乎我的错误是在与调用 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.