如何实现使用最少资源定期检查的线程? [英] How to implement thread which periodically checks something using minimal resources?

查看:142
本文介绍了如何实现使用最少资源定期检查的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在后台运行一个线程,它将以给定的时间间隔检查与某些服务器的连接。例如每5秒钟。

I would like to have a thread running in background which will check connection to some server with given time interval. For example for every 5 seconds.

我不知道这是否有一个很好的desing模式?如果我记得正确的话,我已经读过某处,执行方法中的睡眠线程不好。但是我可能错了。

I don't know if there is a good "desing pattern" for this? If I remember corretly, I've read somewehere that sleeping thread in its execute method is not good. But I might be wrong.

另外,我可以使用正常的TThread类或OTL线程库。

Also, I could use normal TThread class or OTL threading library.

想法?

谢谢。

推荐答案

使用 WaitForSingleObject TThread 后代的执行方法c>等待事件,指定超时。这样,您可以在需要时立即唤醒线程,例如终止时。

You could use an event and implement the Execute method of the TThread descendant by a loop with WaitForSingleObject waiting for the event, specifying the timeout. That way you can wake the thread up immediately when needed, e.g. when terminating.

这篇关于如何实现使用最少资源定期检查的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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