Delphi线程等待数据,处理它,然后恢复等待
我需要在 Delphi 中创建一个具有以下特征的线程:
- 等待主线程将数据添加到共享队列。
- 处理队列中的所有数据,将结果返回到主线程(对于最后一部分,我将仅将消息发送到主窗口)。处理非常耗时,因此当工作线程处理先前的条目时,可能会将新数据添加到队列中。
- 恢复等待,使用尽可能少的 CPU 周期。
我无法向线程发送消息,因为它没有窗口句柄。
我应该使用 WaitForObject 的某种变体吗?如果是这样,还等什么?如果没有,那么我如何保持线程等待,然后在新数据进入队列时唤醒它?
我已阅读多线程 - Delphi 方式,这似乎没有回答我的问题。也许 OmniThreadLibrary 可以满足我的需要;我无法判断,因为文档很少。一般来说,我对线程了解不够,无法弄清楚该库是否会在这里提供帮助以及如何使用它(甚至为什么要使用它而不是仅仅使用 TThread 后代)。
I need to create a thread in Delphi with the following characteristics:
- Waits until the main thread adds data to a shared queue.
- Processes all the data in the queue, returning the results to main thread (for this last part I'll just send messages to the main window). Processing is time-consuming, so new data may be added to the queue while the worker thread is processing previous entries.
- Resumes waiting, using as little cpu cycles as possible.
I cannot send messages to the thread, since it does not have a window handle.
Should I be using some variant of WaitForObject? If so, what would the wait be for? If not, then how can I keep the thread waiting, then awake it when new data comes down the queue?
I've read Multithreading - The Delphi Way, which doesn't seem to answer my question. Perhaps OmniThreadLibrary can do what I need; I can't tell since there's little documentation. I don't know enough about threads in general to figure out if the library will help here and how to use it (or even why to use it instead of just working with TThread descendants).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
OmniThreadLibrary 绝对可以在这方面为您提供帮助。 OTL 发行版中的测试 5 应该可以帮助您入门。
在此演示中,“开始”按钮创建线程并设置一些参数和计时器(如果不需要,您可以在代码中删除它们)。 “更改消息”向线程发送一条消息,该消息在线程的 OMChangeMessage 方法中处理。然后,线程将一些信息发送回客户端(本演示中为 OMSendMessage,但您可以在您将在其中执行工作的同一消息中执行此操作),并且主线程通过 OmniEventMonitor 组件接收此消息。 “停止”按钮停止工作线程。
如果在您的线程繁忙时有更多消息到达,它们将在您的工作方法完成其工作后立即排队并进行处理。当无事可做时,线程将使用进程中的零 CPU 周期等待下一条消息。
编辑
在Delphi 2009及更高版本中,后台工作人员模式提供了更简单的解决方案。
OmniThreadLibrary can definitely help you here. Test 5 from the OTL distribution should help you started.
In this demo, "Start" button creates the thread and sets some parameters and timer (which you can remove in your code if not needed). "Change message" sends a message to the thread and this message is processed in thread's OMChangeMessage method. Thread then sends some information back to the client (OMSendMessage in this demo, but you can do this in the same message you'll be doing your work in) and main thread receives this message via the OmniEventMonitor component. "Stop" button stops the worker thread.
If more messages arrive while your thread is busy, they will be queued and processed as soon as your worker method has completed its work. When there's nothing to do, thread will wait for the next message using zero CPU cycles in the process.
EDIT
In Delphi 2009 and above, the Background Worker pattern provides a simpler solution.
WaitForSingleObject() 可以等待多种类型的同步对象。您可以使用 Windows“事件”同步对象(与 Delphi 事件无关)。您创建事件(SyncObjs、IIRC 中有一个 Delphi TEvent 包装器),然后调用 WaitForSingleObject 等待该事件发出信号。当您必须唤醒线程时,您可以调用 SetEvent 将事件置于有信号状态,然后 WaitForSingleObject 返回。您可以使用 WaitForMultipleObjects() 让线程等待多个对象中的一个(或全部) - 它还会告诉您哪个对象已发出信号。
WaitForSingleObject() can wait on several types of synchronization objects. You can use a Windows "event" synchronization object (which has nothing to do with a Delphi event). You create the event (there's a Delphi TEvent wrapper in SyncObjs, IIRC), and call WaitForSingleObject to wait for that event to become signalled. When you have to awake the thread, you call SetEvent to put the event in the signalled state and WaitForSingleObject returns. You can have a thread wait for one (or all) of multiple object using WaitForMultipleObjects() - it will also tell you which object became signalled.
您绝对可以向线程发送消息,即使它没有窗口句柄。只需使用
PostThreadMessage()
而不是SendMessage()
或PostMessage()
。如果您在 [delphi] 标记中搜索PostThreadMessage()
,则会在 StackOverflow 上找到更多信息 - 我认为在这里复制所有内容并不是一个好主意。但如果您不了解线程编程,那么从 OTL 而不是低级的东西开始可能确实是一件好事。
You can definitely send messages to a thread, even though it doesn't have a window handle. Just use
PostThreadMessage()
instead ofSendMessage()
orPostMessage()
. There will be more information here on StackOverflow if you search forPostThreadMessage()
in the [delphi] tag - I don't think it's a good idea to duplicate everything here.But if you are not knowledgeable about thread programming, then starting with OTL instead of the low level stuff may indeed be a good thing.
这是一个简单的例子,告诉你如何做到这一点......
Here's a simple example how you can do it...