处理实时时钟的最佳方式?
我正在用 C 语言为 Linux 平台构建一个应用程序,带有 GTK+2 GUI。我需要在屏幕上运行一个实时时钟,显示 HH:MM:SS(只需精确到秒),然后在用户在 GTK_ENTRY 字段中输入的特定时间触发各种功能。
我一直在研究执行此操作的最佳方法,并且发现了类似的问题,人们已经回答使用 GThread 线程来监视时间,但我无法找到任何具体细节。
我可以向线程发送一个包含无限循环、不断检查时间的函数吗?不知何故,这似乎不对,或者说是处理它的最佳方式。
我真的很感激任何人可以提供的任何细节,或者任何可能回答这个问题的资源方向。
谢谢你!
I'm building an application for a linux platform in C, with a GTK+2 GUI. I need to keep a real-time clock running on the screen displaying HH:MM:SS (only needs to be accurate to the second), and then have various functions triggered at specific times entered by the user in a GTK_ENTRY field.
I've been researching the best way to do this, and have found similar questions where people have answered to use GThread threading to monitor the time, but I have not been able to find any specifics.
Can I send a function that contains an endless loop, constantly checking the time over and over, to a thread? Somehow that doesn't seem right, or the best way to handle it.
I would really appreciate any specifics anyone can offer, or any direction towards resources that might answer this question.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需添加一个 glib 超时处理程序,使用
g_timeout_add()
。如果您使用一段时间,例如半秒,应该没问题。在处理程序代码中,执行所需的任何小部件操作以刷新显示的时间。这可能就像为 GtkLabel 格式化一些文本一样简单。当然,我不建议您使用超时回调的“滴答声”作为实际时钟源,您应该使用常规 API:s 来找出每个回调中的时间。
Simply add a glib timeout handler, using
g_timeout_add()
. If you use a period of, say, half a second you should be fine. In the handler code, do whatever widget manipulation is needed in order to refresh the displayed time. This might be as simple as formatting some text for a GtkLabel.Of course, I don't recommend that you use the "ticking" of the timeout callbacks as the actual clock source, you should use regular API:s to find out what time it is in each callback.