TThread 创建以 TPanel 为父级的 TButton
我有一个关于在 MainThread TPanel
上单独创建 MainThread TButton
的 TThread
的问题。 TPanel
必须设置为 TButton
的父级。
ButtonVariableName := TButton.Create (
(Form1.FindComponent('PanelNameString') as TComponent)
);
ButtonVariableName.Parent := (
(Form1.FindComponent('PanelNameString') as TWinControl)
);
不起作用...
ButtonVariableName
位于主线程上。 TButton.Create()
正在单独的 TThread 上调用。 ButtonVariableName.Parent
也从单独的TThread
调用。
FindComponent
似乎是崩溃的原因。当我将其移除并在那里放置其他东西时,它就可以工作了。从单独的 TThread
调用时,FindComponent
可能不起作用,但我不确定。
有什么指点^? 哈哈。
-i2程序员
I have a question about Separate TThread
creation of a MainThread TButton
on a MainThread TPanel
. The TPanel
must be set as the TButton
's Parent.
ButtonVariableName := TButton.Create (
(Form1.FindComponent('PanelNameString') as TComponent)
);
ButtonVariableName.Parent := (
(Form1.FindComponent('PanelNameString') as TWinControl)
);
is not working...
ButtonVariableName
is on MainThread.TButton.Create()
is being called on Separate TThread.ButtonVariableName.Parent
is also called from Separate TThread
.
FindComponent
seems to be what is breaking down. When I remove it and place something else there it works. It may be that FindComponent
doesn't work when called from Separate TThread
but I'm not certain.
Any pointers^?
LOL.
-i2programmer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能从辅助线程使用 VCL。在辅助线程中使用 Synchronize 或 Queue 在主线程上下文中执行 VCL 相关代码。
You can not use VCL from secondary threads. Use Synchronize or Queue in the secondary thread to execute VCL-related code in the context of the main thread.
这应该是一条评论,但我想包含一些代码。首先,您不应该从辅助线程调用任何 VCL,因此不能保证调用
FindComponent
能够正常工作。尽管如此,我怀疑这是你的问题,因为除非你特别幸运,否则你不会得到竞争条件,所以你不会得到错误。您应该做两件事:
FindComponent
很容易测试并确定时,无需猜测它是否会失败。像这样分解你的代码:
This was supposed to be a comment but I want to include some code. First of all you're not supposed to call any VCL from secondary threads, so calling
FindComponent
is not guaranteed to work. Despite this fact, I doubt this is your problem, because unless you're especially lucky you will not get a race condition so you will not get an error.You should do two things:
FindComponent
is the one that's failing when it's so easy to test and be sure.Brake up your code like this: