如何创建计时器? (每X秒/分/小时做某事) [英] How do I create a timer? (Do something every X seconds/minutes/hours)

查看:221
本文介绍了如何创建计时器? (每X秒/分/小时做某事)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的程序每10秒做一些事情,我不知道如何在C ++控制台应用程序。在C#中我可以创建一个计时器,但我现在正在努力...

I need to do something in my program every 10 seconds and I don't know how to do it in C++ Console Application. In C# I could just create a timer but I'm struggling here now...

sleep();不是一个选项,因为据我所知它使你的程序不活动X秒,我需要我的应用程序是活跃的和工作,所有的时间计算。

sleep(); is not an option because as far as I know it makes your program inactive for X seconds, I need my app to be active and working, calculating all the time.

请帮助我这个小(或大?)问题,我很新的C + +和学习ATM:)

Please help me with this little (or big?) problem, I'm very new to C++ and learning ATM :)

所以我设置了BOOST库,现在我'm做这样的事情。

So I've set up BOOST libraries and now I'm doing something like this.

boost::timer t;

while (true)
{
    if (t.elapsed() > 1)
    {
        cout << t.elapsed() << endl;
        t.restart();
    }
}

?哦,不,这是非常糟糕,吃我25%的CPU不停止。 :(

Is it good enough or there should be much better way? Oh no, it's very bad, eating 25% of my CPU non-stop. :(

推荐答案

Boost有一个计时器库。

Boost has a timer library.

  • http://www.boost.org/doc/libs/1_45_0/libs/timer/index.html

其实,如果你没有遇到Boost已经,我相信你会发现它非常有用。

In fact, if you haven't come across Boost already, I'm sure you'll find it very useful.

要做一个基于事件的计时器,请查看此答案

To do an event based timer, you should look at this answer.

这篇关于如何创建计时器? (每X秒/分/小时做某事)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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