Delphi中的跨线程通信 [英] Cross thread communication in Delphi

查看:71
本文介绍了Delphi中的跨线程通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi中是否有有关跨线程通信的文档?如何将消息发送到没有窗口的线程?

Is there any documentation on cross thread communication in Delphi? How can I send message to the thread that doesn't have a window?

推荐答案

您只能将(Windows)消息发送到实现标准消息循环的线程,该线程将在实现窗口句柄后自动创建.

You can only send (Windows) messages to threads that implement a standard message loop, which will automatically be created once a window handle is realized.

但是,不必使用消息与线程进行通信.只需让它在事件对象(VCL中为TEvent)上等待,并在您希望线程执行功能时发信号通知该事件.

It is however not necessary to use messages to communicate with a thread. Just let it wait on an event object (TEvent in VCL), and signal this event when you want the thread to perform a function.

但是,如果您不熟悉多线程,请不要自己研究所有这些细节,除非您想获得学习效果.只需使用 OmniThreadLibrary 并完成此操作即可.一旦您知道如何使用它,就可以通过深入研究它的内部知识来学习很多东西.

But if you are new to multi-threading - don't go into all these details on your own, unless you want to for the learning effect. Just use the OmniThreadLibrary and be done with it. There's much good to be learned by digging into its internals, once you know how to use it.

另请参阅

关于询问"[OmniThreadLibrary]会使什么变得更容易并且以什么成本进行?"的评论.我只能建议您亲自检查一下-也就是说,如果您至少使用的是Delphi2007.有几个示例可以说明这些概念,但是对于快速的真实"示例,您可以看看此博客文章-您甚至不需要为此安装库.

Regarding the comment asking "What does [OmniThreadLibrary] make easier, and at what cost?" I can only advise you to check it out for yourself - that is if you are using at least Delphi 2007. There are several samples to illustrate the concepts, but for a quick "real-life" example you could have a look at this blog post - you don't even need to install the library for that.

我也同意使用库进行多线程处理确实需要一定的信念. OTOH几乎不能替代VCL提供的功能.该示例代码仍然使用不正确的Synchronize()调用.不支持诸如线程安全的生产者-消费者队列之类的东西,这些东西更适合于多线程编程.而且,如果您同意与VCL相比,您的多线程程序需要更坚实的基础-为什么要重新发明特定的轮子呢?

I do also agree that using a library for multi-threading does require a certain act of faith. OTOH making do with what the VCL provides is hardly an alternative. The sample code does still use the ill-conceived Synchronize() call. There is no support for things like thread-safe producer-consumer-queues, which are much more suited to multi-threaded programming. And if you do agree that you need a more solid fundament for your multi-threaded programs than the VCL provides - why reinvent that particular wheel?

关于使用该库的成本:您是否需要为它计时是否足够快.它确实以一种很好的方式对线程之间的通信进行了抽象,恕我直言,但是显然每个抽象都会牺牲性能.

As for the cost of using the library: You will have to time yourself whether it is fast enough for you. It does abstract the communication between threads in a good way IMHO, but every abstraction costs performance, obviously.

如果您认为不适合您,请亲自编写代码.我为Delphi 4做过同样的事情,并且我已经使用该代码近10年了.从我当时发现的错误数量和遇到的极端情况来看,我绝对建议任何不熟悉多线程的人都为其编写自己的库代码.如果您确实真的想要,请遵循

If you decide that it is not for you after all - write the code yourself. I did the same for Delphi 4, and I have been using that code for nearly 10 years now. And judging by the amount of bugs I found and corner cases I experienced in that time, I would definitely advise anybody new to multi-threading to not write their own library code for it. And if you really really want to, please take the rules in this posting to heart.

这篇关于Delphi中的跨线程通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆