如何实现使用最少资源定期检查的线程?

编程入门 行业动态 更新时间:2024-10-09 18:18:41
本文介绍了如何实现使用最少资源定期检查的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在后台运行一个线程,它将以给定的时间间隔检查与某些服务器的连接。例如每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 等待事件,指定超时。这样,您可以在需要时立即唤醒线程,例如终止时。

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.

更多推荐

如何实现使用最少资源定期检查的线程?

本文发布于:2023-11-30 21:45:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   如何实现   定期检查   资源

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!