暂停线程的执行而不休眠?
我正在使用 Skype API,它每次收到一条消息都会发回一条消息。我不确定这是否真的是导致它的原因,但这是我能得到的最接近的结果:当我发送太多消息时,COM 控件无法处理所有回复,这会导致它使整个应用程序崩溃。当我使用 for
循环时就会发生这种情况。
我正在使用线程来完成这项工作,所以我的程序不会挂起。我知道我可以在线程中执行 Sleep();
,并且不会(应该)使整个程序休眠。但问题是,我的 COM 控件也将处于休眠状态,因此它仍然无法处理它需要的任何内容,因此它可以再次跟上。
所以,问题是:如何在不暂停整个线程的情况下暂停例程,以便我的 COM 对象可以处理回复而不会重载?
I am using the Skype API, which sends back a message everytime it receives one. I am not sure if this really is what is causing it, but it's the closest I can get: When I send too many messages, the COM control can't handle all the replies, which causes it to crash the whole app. That happens when I use a for
loop.
I am using Threads to do the job, so my program won't hang. I know I can do Sleep();
in the thread, and will (should) not make the whole program sleep. The problem is though, that my COM control will be sleeping aswell, so it still wont be able to process whatever it needs, so it can keep up again.
So, the question is: How can I pause the routine without pausing the whole thread, so that the replies can be processed by my COM object, without overloading?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CoWaitForMultipleHandles 可用于阻止线程,但仍然泵送 COM 消息(在 STA 的情况下)、一些其他 Windows 消息并且有超时。听起来像是你可以使用的东西。
CoWaitForMultipleHandles can be used to block the thread, but still pump COM messages (in case of STA), some other Windows messages and has a timeout. Sounds like something you could use.
对于这种情况,我倾向于使用 TSimpleEvent。它可能会阻塞您的线程一段时间(超时),但您也可以在外部用它唤醒您的线程(例如,在调用 Terminate 之前)。它不消耗CPU。它可能会被其他线程的调用解除阻塞。我将其用于必须表现得像定时的线程,例如每 5 秒唤醒一次并执行某些操作。
*)我不明白你的确切问题,但我回答的是在线程中暂停而不睡眠。也许它可以用作您的解决方案(的一部分)。祝你好运。
I tend to use TSimpleEvent for such cases. It may block your thread for a set amount of time (the time-out), but you may also wake up your thread with it externally (e.g. right before your call Terminate). It doesn't cost CPU. It may be deblocked by calls from other threads. I use this for threads that have to behave as if they are timed, e.g. wake up every 5 seconds and do something.
*) I Don't grasp your exact problem, but pausing in threads without sleeps is what I answered to. Maybe it can be used as (part of) your solution. Good luck with it.
听起来线程需要测试每次调用时是否应该发送消息,而不是总是发送消息。
如何做到这一点取决于实施;您可能有某种需要满足的条件(大小阈值,因此您永远不会发送非常小的消息),或者可能通过速率限制(检查最后一条消息发送的时间是否足够长,您有能力发送其他)。
我希望这对您有所帮助;我知道这是非常高水平的。
Sounds like the thread needs to test whether it should send a message each time it is called, rather than always sending one.
How to do this depends on the implementation; you may have some kind of condition that needs to be met (a size threshold, so you never send a message that is trivially small), or perhaps through rate limiting (check whether the last message sent long enough ago that you can afford to send another).
I hope this is helpful; I know it is very high-level.
您可以使用两个线程,一个用于发送,一个用于接收,然后像 在此消费者/生产者示例中(“Delphi 中与受保护块的线程同步”,适用于 Delphi 2009 及更高版本),以便生成一条新消息只能等消费者处理完之后生产者才可以发送?
Could you use two threads, one for sending, one for receiving, and lock them like in this consumer/producer example ("Thread Synchronization with Guarded Blocks in Delphi", for Delphi 2009 and higher), so that a new message can only be sent by the producer when the consumer has processed it?