每个 VCL 表单都应该有自己的消息循环/线程泵吗?
我正在尝试在我最新的项目中实现 MVP 模式。目前使用的是 C++ Builder 2007 附带的 VCL 库。我的想法是我不需要执行 Application->Run(),或者更糟糕的 Application->CreateForm(),它创建一个主窗体并在该窗体上循环。我不需要主表单,我想要一个主演示者。
我的问题是如何创建线程 TForm?
选项 1: 如果只有一个消息循环(演示者),那么我系统中的每个随机线程都必须向该主线程发布一条消息并让它创建表单。
选项 2: 每个表单都有自己的消息循环。现在随机线程可以根据需要新建和删除它们。他们之间仍然使用发布消息进行通信。
如果建议选择 2,是否有人对实施此方法有任何建议?
编辑: 我如何更改以下内容以允许使用 new 创建表单并仍然允许循环工作?
// Start VCL library
pApplication->Initialize();
// Create the Main form and assign the MainForm property
pApplication->CreateForm(__classid(TForm1), &pFormMain);
// Show the form
pFormMain->Show();
// Run the loop
pApplication->Run();
I'm attempting to implement a MVP pattern in my latest project. Currently using the VCL library that comes with C++ Builder 2007. My thinking is I don't need to do Application->Run(), or worse Application->CreateForm() which creates a main form and loop on that form. I don't want a main form, I want a main Presenter, instead.
My question then becomes how to create threaded TForms?
Option 1:
If there is only one message loop (the Presenter) then every random thread in my system would have to post a message to this main thread and have it create forms.
Option 2:
Every form has its own message loop. Now random threads can new and delete them as needed. Posting messages is still used for communications between them.
If option 2 is recommended does anyone have any advice on implementing this approach?
EDIT:
How might I change the following to allow for creating the form using new and still allow the loop to work?
// Start VCL library
pApplication->Initialize();
// Create the Main form and assign the MainForm property
pApplication->CreateForm(__classid(TForm1), &pFormMain);
// Show the form
pFormMain->Show();
// Run the loop
pApplication->Run();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不必将每个表单放入其自己的线程中。您将每个表单放在主线程中。
一个线程只有一个消息循环。例外情况是当表单以模态方式显示时运行的特殊消息循环。
pApplication->Run();
运行消息循环。当发布的消息被处理时,它们被分派到适当的窗口过程。发送消息时,它们会直接同步传递到窗口过程。您可以创建和显示任意数量的表单,并通过同一个消息循环为它们提供服务。你不仅可以这样做,而且这是做事的方式。
如何将这些知识映射到 MVP 框架上是另一回事,但在单个线程中运行 GUI 是任何解决方案中的一个固定点。
编辑
您询问如何使用 VCL 在没有可见的主窗体的情况下运行消息循环。您有两个选择:
pApplication->Run();
之前创建一个不可见的表单。在我看来,选项 1 是迄今为止更好的选择。
You don't put each form in its own thread. You put each form in the main thread.
A thread only has one message loop. The exception to this is the special message loop that is run when a form is shown modally.
pApplication->Run();
runs your message loop. When posted messages are processed they get dispatched to the appropriate window procedure. When messages are sent they are delivered synchronously direct to the window procedure.You can create and show as many forms as you like and service them all from the same message loop. Not only can you do this, it is the way to do things.
How you map this knowledge onto your MVP framework is another matter, but running your GUI out of a single thread is a fixed point in any solution.
EDIT
You ask how, with VCL, to run a message loop if you don't have a visible main form. You have two options:
pApplication->Run();
.In my view option 1 is by far the better option.
您无法使用 VCL 安全地创建线程表单,因为 VCL 不是线程安全的。
此外,每个表单已经包含它自己的消息循环。 TApplication 只是将消息分派到每个表单的循环。
You can't safely create threaded forms using the VCL, because the VCL isn't thread-safe.
Also, each form already contains it's own message loop. TApplication simply dispatches messages to each form's loop.